Wasi
Wasi

Reputation: 751

Image to Byte array conversion

I need to convert image to Byte array as I have to upload to server, have seen many articles but i am not able to understand ho wit is done, forgive me if you think my question is up-to be asked in this forum

Upvotes: 0

Views: 736

Answers (2)

Igor Ralic
Igor Ralic

Reputation: 15006

If you have a WriteableBitmap, for example called LoadedPhoto, you can try:

using (MemoryStream ms = new MemoryStream()
{
    LoadedPhoto.SaveJpeg(ms, LoadedPhoto.PixelWidth, LoadedPhoto.PixelHeight, 0, 95);
    ms.Seek(0, 0);
    byte[] data = new byte[ms.Length];
    ms.Read(data, 0, data.Length);
    ms.Close();
}

If you're using WriteableBitmapEx library from CodePlex, it can do a conversion to byte array, too http://writeablebitmapex.codeplex.com/

Upvotes: 1

MBen
MBen

Reputation: 3996

I assume that it is a Bitmap (however it works for all image types) :

 System.Drawing.Bitmap bmp = GetYourImage();
 System.IO.MemoryStream stream = new System.IO.MemoryStream();
 bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
 stream.Position = 0;
 byte[] data = stream.ToArray();

Upvotes: 0

Related Questions