Skip to main content
Home Forums Silverlight Programming Visual Studio & Silverlight Development Tools bitmap image to byte[] array
11 replies. Latest Post by fblin on November 6, 2009.
(0)
satin
Member
4 points
26 Posts
03-05-2009 5:20 AM |
Hello !
Please teach me how to convert bitmap image to byte[] array?
Thanks in advance!
nasiraziz
235 points
88 Posts
03-06-2009 2:49 AM |
EDIT: This is not SL Solution. OP, please unmark this as answer. Thank you
byte[] BmpToBytes (Bitmap bmp) { MemoryStream ms = new MemoryStream(); bmp.Save (ms, ImageFormat.Jpeg); byte[] bmpBytes = ms.GetBuffer(); bmp.Dispose(); ms.Close(); return bmpBytes; }
hariram....
181 points
50 Posts
03-09-2009 1:16 PM |
public byte[] imageToByte(System.Drawing.Image img){MemoryStream objMS = new MemoryStream();img.Save(objMS,System.Drawing.Imaging.ImageFormat.Gif);return objMS.ToArray();}
pkr2000
Participant
1219 points
377 Posts
03-29-2009 11:45 AM |
Are you sure any of the above work in SL2?
theotherjay
20 points
23 Posts
04-02-2009 2:28 PM |
The previous "answers" to this post are incorrect.
There is no System.Drawing.Imaging namespace in Silverlight.
There is no "Bitmap" class in Silverlight.
There is no "save" method in the System.Windows.Media.Imaging.BitmapImage class nor is there one in the Image class.
04-02-2009 5:20 PM |
My understanding is that the question is not specific to Silverlight (Although its a SL forum). Yes the answers are not valid for Silverlight. Maybe the poster is trying to do it on WebServices level or somewhere else. Read the question again.
Cheers
04-02-2009 5:32 PM |
satin: Hello ! Please teach me how to convert bitmap image to byte[] array? Thanks in advance!
Without a reference otherwise, Silverlight is the assumed target. I just pointed out the fact that bitmap to byte[] cannot be done in Silverlight. The person who posted prior to my last post was unsure of this.
04-03-2009 5:15 PM |
My question is exactly silverlight question. And I agree with all above peoples who told - these post were incorrect. Yes, there is no method in the System.Windows.Media.Imaging.BitmapImage class nor is there one in the Image class. Therefore I actually asked about it.
ksleung
Contributor
5366 points
1,028 Posts
04-05-2009 2:24 AM |
Satin,
I think the answer is more than just whether these classes are not available. I believe saving image to byte[] will eventually become available although it won't be through the SL team adding support to these namespaces.
For security reason I think all you have access to in the near future is the WriteableBitmap class, not the Bitmap class. You can access the bits of the WriteableBitmap very easily, and you can "render' the content of an image to a WriteableBitmap by calling WriteableBitmap.Render(). The caveat is that if you do that, you don't have access to the content of WriteableBitmap for security reason... In SL3 RTW hopefully this will be addressed.
Now, even if you have access to the Bitmap in say SL3 RTW, you will still not have the save() function. For the reason of maintaining a reasonable SL runtime size, these "luxury" APIs are simply not going to be there, IMHO. So, at the end of the day you'll have to roll your own Jpeg/Bmp/Gif encoder -- not necessarily that difficult -- or let your web service deal with it.
04-05-2009 2:28 AM |
For more details about this topic, see http://silverlight.net/forums/p/83709/194891.aspx and http://silverlight.net/forums/t/82223.aspx
05-03-2009 1:07 PM |
If you're using the Open Dialog to fetch your image then you can maintain two variables and avoid the conversion problem (please ignore my cheesy handling of readbytes);
BitmapImage imageSource = new BitmapImage();
Stream stream = openFileDialog.File.OpenRead();
BinaryReader binaryReader = new BinaryReader(stream);
byte[] currentImageInBytes = binaryReader.ReadBytes((int)stream.Length);
stream.Position = 0;
imageSource.SetSource(stream);
this.ImagePicture.Source = imageSource;
fblin
12 Posts
11-06-2009 1:31 PM |
pkr2000... your suggestion worked fine in my case with silverlight3!
Thank