Skip to main content

Microsoft Silverlight

Answered Question bitmap image to byte[] array RSS Feed

(0)

satin
satin

Member

Member

4 points

26 Posts

bitmap image to byte[] array

Hello !

Please teach me  how to convert bitmap image to byte[] array?

Thanks in advance!

nasiraziz
nasiraziz

Member

Member

235 points

88 Posts

Re: bitmap image to byte[] array

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;

}
  

-------------------------------------------------
{DeVigner} :: http://blog.geomentary.com/

hariram.p@live.in
hariram....

Member

Member

181 points

50 Posts

Answered Question

Re: bitmap image to byte[] array

public byte[] imageToByte(System.Drawing.Image img)
{
MemoryStream objMS = new MemoryStream();
img.Save(objMS,System.Drawing.Imaging.ImageFormat.Gif);
return  objMS.ToArray();
}

Thanks
Hari
---------------------------------------------
Dont forget to click "Mark as Answer" on the post that helped you.

My Site

pkr2000
pkr2000

Participant

Participant

1219 points

377 Posts

Re: bitmap image to byte[] array

Are you sure any of the above work in SL2?

 

theotherjay
theotherjay

Member

Member

20 points

23 Posts

Re: bitmap image to byte[] array

 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.

nasiraziz
nasiraziz

Member

Member

235 points

88 Posts

Re: bitmap image to byte[] array

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

-------------------------------------------------
{DeVigner} :: http://blog.geomentary.com/

theotherjay
theotherjay

Member

Member

20 points

23 Posts

Re: bitmap image to byte[] array

 

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.

satin
satin

Member

Member

4 points

26 Posts

Re: bitmap image to byte[] array

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
ksleung

Contributor

Contributor

5366 points

1,028 Posts

Re: bitmap image to byte[] array

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.

ksleung
ksleung

Contributor

Contributor

5366 points

1,028 Posts

Re: bitmap image to byte[] array

For more details about this topic, see http://silverlight.net/forums/p/83709/194891.aspx and http://silverlight.net/forums/t/82223.aspx

pkr2000
pkr2000

Participant

Participant

1219 points

377 Posts

Re: bitmap image to byte[] array

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
fblin

Member

Member

4 points

12 Posts

Re: Re: bitmap image to byte[] array

pkr2000...  your suggestion worked fine in my case with silverlight3!Smile

Thank

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities