This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17804|Reply: 0

[WPF] Operation of image files, byte[], and BitmapImage in WPF

[Copy link]
Posted on 12/16/2015 5:00:21 PM | | |
  1. public MainWindow()
  2.         {
  3.             InitializeComponent();
  4.             byte[] b = GetPictureData(@"F:\WPF\TestSolution\TestReatByteFromDB\Images\123.png");
  5.             BitmapImage myimg = ByteArrayToBitmapImage(b);
  6.             this.testImg.Source = myimg;
  7.         }

  8.         public byte[] GetPictureData(string imagepath)
  9.         {
  10.             /**/
  11.             ////根据图片文件的路径使用文件流打开,并保存为byte[]
  12.             FileStream fs = new FileStream(imagepath, FileMode.Open);//可以是其他重载方法
  13.             byte[] byData = new byte[fs.Length];
  14.             fs.Read(byData, 0, byData.Length);
  15.             fs.Close();
  16.             return byData;
  17.         }

  18.         public BitmapImage ByteArrayToBitmapImage(byte[] byteArray)
  19.         {
  20.             BitmapImage bmp = null;

  21.             try
  22.             {
  23.                 bmp = new BitmapImage();
  24.                 bmp.BeginInit();
  25.                 bmp.StreamSource = new MemoryStream(byteArray);
  26.                 bmp.EndInit();
  27.             }
  28.             catch
  29.             {
  30.                 bmp = null;
  31.             }

  32.             return bmp;
  33.         }

  34.         public byte[] BitmapImageToByteArray(BitmapImage bmp)
  35.         {
  36.             byte[] byteArray = null;

  37.             try
  38.             {
  39.                 Stream sMarket = bmp.StreamSource;

  40.                 if (sMarket != null && sMarket.Length > 0)
  41.                 {
  42.                     //很重要,因为Position经常位于Stream的末尾,导致下面读取到的长度为0。
  43.                     sMarket.Position = 0;

  44.                     using (BinaryReader br = new BinaryReader(sMarket))
  45.                     {
  46.                         byteArray = br.ReadBytes((int)sMarket.Length);
  47.                     }
  48.                 }
  49.             }
  50.             catch
  51.             {
  52.                 //other exception handling
  53.             }

  54.             return byteArray;
  55.         }
Copy code






Previous:WPF changes the default items for the launch window
Next:Be careful to write try catch outside of while! and write return in while!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com