ImagesManip.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Windows.Media.Imaging;
  5. namespace Cafe
  6. {
  7. internal class ImagesManip
  8. {
  9. public static byte[] BitmapSourceToByteArray(BitmapSource image)
  10. {
  11. #region Кодирование картинки
  12. using (var stream = new MemoryStream())
  13. {
  14. var encoder = new PngBitmapEncoder();
  15. encoder.Frames.Add(BitmapFrame.Create(image));
  16. encoder.Save(stream);
  17. return stream.ToArray();
  18. }
  19. #endregion
  20. }
  21. public static BitmapImage SelectImage()
  22. {
  23. #region Выбор картинки
  24. OpenFileDialog op = new OpenFileDialog();
  25. op.Title = "Выбрать изображение";
  26. op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
  27. "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
  28. "Portable Network Graphic (*.png)|*.png";
  29. if (op.ShowDialog() == true)
  30. return new BitmapImage(new Uri(op.FileName));
  31. else
  32. return null;
  33. #endregion
  34. }
  35. public static BitmapImage NewImage(Dishes dish)
  36. {
  37. #region Декодирование картинки
  38. MemoryStream ms = new MemoryStream(dish.Picture);
  39. BitmapImage image = new BitmapImage();
  40. image.BeginInit();
  41. image.StreamSource = ms;
  42. image.EndInit();
  43. return image;
  44. #endregion
  45. }
  46. }
  47. }