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