开发者

How to Convert system.windows.controls.image to byte[]

开发者 https://www.devze.com 2023-02-19 16:49 出处:网络
I need to convert System.Windows.Controls.Image to byte[]开发者_如何学Python. How to do it ? Assuming that you answer my query above that you are actually trying to convert an image into a byte arra

I need to convert System.Windows.Controls.Image to byte[]开发者_如何学Python.

How to do it ?


Assuming that you answer my query above that you are actually trying to convert an image into a byte array rather than an image control into a byte array here is the code to do so;

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 return  ms.ToArray();
}


In general, to convert an object to a byte array, you may want to use binary serialization. This will generate a memory stream from which you can extract a byte array.
You may start with this Question and Answer


System.Windows.Controls.Image img = new System.Windows.Controls.Image();
var uri = new Uri("pack://application:,,,/Images/myImage.jpg");
img.Source = new BitmapImage(uri);
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
      var bmp = img.Source as BitmapImage;
      JpegBitmapEncoder encoder = new JpegBitmapEncoder();
      encoder.Frames.Add(BitmapFrame.Create(bmp));
      encoder.Save(ms);
      arr = ms.ToArray();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消