SomeMethods.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Media.Imaging;
  8. namespace Hotel_Course_Project
  9. {
  10. class SomeMethods
  11. {
  12. public static BitmapImage ConvertByteToBitmapImage(byte[] array)
  13. {
  14. using (var ms = new System.IO.MemoryStream(array))
  15. {
  16. var image = new BitmapImage();
  17. image.BeginInit();
  18. image.CacheOption = BitmapCacheOption.OnLoad;
  19. image.StreamSource = ms;
  20. image.EndInit();
  21. return image;
  22. }
  23. }
  24. public static byte[] ConvertBitmapImageToByte(BitmapSource image)
  25. {
  26. using (var stream = new MemoryStream())
  27. {
  28. var encoder = new PngBitmapEncoder();
  29. encoder.Frames.Add(BitmapFrame.Create(image));
  30. encoder.Save(stream);
  31. return stream.ToArray();
  32. }
  33. }
  34. }
  35. }