ImagesManip.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Windows.Media.Imaging;
  5. namespace MyTests
  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. {
  26. Title = "Выбрать изображение",
  27. Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
  28. "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
  29. "Portable Network Graphic (*.png)|*.png"
  30. };
  31. if (op.ShowDialog() == true)
  32. return new BitmapImage(new Uri(op.FileName));
  33. else
  34. return null;
  35. #endregion
  36. }
  37. //public static BitmapImage NewImage(Parts part)
  38. //{
  39. // MemoryStream ms = new MemoryStream(part.Image);
  40. // BitmapImage image = new BitmapImage();
  41. // image.BeginInit();
  42. // image.StreamSource = ms;
  43. // image.EndInit();
  44. // return image;
  45. //}
  46. }
  47. }