SomeMethods.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.IO;
  2. using System.Windows.Media.Imaging;
  3. namespace Hotel_Course_Project
  4. {
  5. public class SomeMethods
  6. {
  7. #region Конвертирование байтового изображения в стандарт
  8. public static BitmapImage ConvertByteToBitmapImage(byte[] array)
  9. {
  10. if (TestconvertBitToImage(array) == true)
  11. {
  12. using (var ms = new System.IO.MemoryStream(array))
  13. {
  14. var image = new BitmapImage();
  15. image.BeginInit();
  16. image.CacheOption = BitmapCacheOption.OnLoad;
  17. image.StreamSource = ms;
  18. image.EndInit();
  19. return image;
  20. }
  21. }
  22. else
  23. {
  24. return null;
  25. }
  26. }
  27. public static bool TestconvertBitToImage(byte[] array)
  28. {
  29. try
  30. {
  31. using (var ms = new System.IO.MemoryStream(array))
  32. {
  33. var image = new BitmapImage();
  34. image.BeginInit();
  35. image.CacheOption = BitmapCacheOption.OnLoad;
  36. image.StreamSource = ms;
  37. image.EndInit();
  38. }
  39. return true;
  40. }
  41. catch
  42. {
  43. return false;
  44. }
  45. }
  46. #endregion
  47. #region Конвертирование стандарт изображения в массив байта
  48. public static byte[] ConvertBitmapImageToByte(BitmapSource image)
  49. {
  50. using (var stream = new MemoryStream())
  51. {
  52. var encoder = new PngBitmapEncoder();
  53. encoder.Frames.Add(BitmapFrame.Create(image));
  54. encoder.Save(stream);
  55. return stream.ToArray();
  56. }
  57. }
  58. #endregion
  59. }
  60. }