I’m able to convert a byte[] to an image:
byte[] myByteArray = ...; // ByteArray to be converted
MemoryStream ms = new MemoryStream(my);
BitmapImage bi = new BitmapImage();
bi.SetSource(ms);
Image img = new Image();
img.Source = bi;
But I’m not able to convert the Image back to a byte[]!
I found in the Internet a solution, that works for WPF:
var bmp = img.Source as BitmapImage;
int height = bmp.PixelHeight;
int width = bmp.PixelWidth;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);
byte[] bits = new byte[height * stride];
bmp.CopyPixels(bits, stride, 0);
The Silverlight libary is so tiny that the class BitmapImage has no property called Format!
Has anybody an idea which solves my problem.
I searched in the internet for a long time to find a solution, but there are is no solution, which works in silverlight!
Thanks!
(the bits per pixel method you are missing just details how the color information is stored per pixel)
As anthony suggested, a WriteableBitmap would be the easiest way – check out http://kodierer.blogspot.com/2009/11/convert-encode-and-decode-silverlight.html for a method to get an argb byte array out :