Silverlight / WPF - Converting an Image into a byte[] array in Silverlight 4
Asked By Brian on 21-Jun-10 05:42 PM
I am looking for a way to convert a JPG image from a Silverlight 4 Image control into a byte[] Most of the things I find on the net assume that the image has a save method but the Silverlight 4 image does not seem to have this.
Any suggestions?
Re : Converting an Image into bute[] array in silverlight 4
Goniey N replied to Brian on 22-Jun-10 09:02 AM
Use Below Code :
private byte[] ConvertImageToByteArray(Image imageToConvert, ImageFormat formatOfImage)
{
byte[]
Ret = new byte[1000];
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, formatOfImage);
Ret
= ms.ToArray();
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
return Ret;
}
\\I Hope This Will helpful To You......
Brian replied to Goniey N on 22-Jun-10 10:53 AM
Thanks for your reply. Unfortunately in SL4 the image control does not seem to have save as one of its methods. I have a crude workaround. Since the image comes from a file (obtained via open dialog) I can use a file stream and get a byte array from that. It is not as functional as the method you suggested but as I said, SL4 does not seem to allow image saves.
I think that has been sandboxed out
Robbe Morris replied to Brian on 26-Jun-10 08:55 PM
For eggheadcafe's silverlight app (
http://www.eggheadcafe.com/silverlight.aspx ), we ended up using a IValueConverter that converts a url to a new BitMapImage. I had an IValueConverter for WPF that converted byte arrays to images and back again. I don't recall ever coming up with a suitable workaround for Silverlight. I'll do some looking through my old code and post if I find something.