WPF Image control source:byte[], BitmapImage convert each other

Source: Internet
Author: User

File converted to byte[]

New FileStream (filepath, FileMode.Open, FileAccess.Read); byte New byte  0, desbytes.length); fs. Close ();  

Byte[] Convert to BitmapImage:

 public  static  BitmapImage     Bytearraytobitmapimage (byte  [] byteArray) {      BitmapImage bmp  = null  ;  try   {BMP  = new   BitmapImage (); Bmp.         BeginInit (); Bmp. Streamsource  = new   MemoryStream (         ByteArray); Bmp.     EndInit ();  catch   {BMP  = null  ;  return   bmp;}  

BitmapImage convert to byte[]:

 Public Static byte[] Bitmapimagetobytearray (BitmapImage bmp) {byte[] ByteArray =NULL; Try{Stream Smarket=bmp.          Streamsource; if(Smarket! =NULL&& smarket.length >0)         {             //is important because position is often at the end of the stream, resulting in a length of 0 read below. Smarket.position =0; using(BinaryReader br =NewBinaryReader (Smarket)) {ByteArray= Br. Readbytes ((int) smarket.length); }         }     }     Catch     {         //Other exception handling    }      returnByteArray;}
New  as BitmapSource); //  byte[] b = convert.frombase64string (Getbase64image (WB)); // get a byte array

WPF Image control source:byte[], BitmapImage convert each other

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.