This project is read-only.

WriteableBitmap To Byte Array

Jan 24, 2011 at 11:23 PM


    I'm trying to convert an WriteableBitmap to a byte array. At this point it's not clear to me if I can do this task without the ImageTools DLLs.  However, even after a fair amount of research I'm not clear on how to proceed and was hoping that someone could provide some guidance.

    Here's what I have so far. While it does create a byte array, when I save the byte array it doesn't create a valid image.

                WriteableBitmap bmp= new WriteableBitmap((int) imgFront.Width, (int) imgFront.Height);
                bmp.Render(imgFront, new MatrixTransform());

                int[] p = bmp.Pixels;
                int len = p.Length * 4;
                byte[] result = new byte[len]; // ARGB
                Buffer.BlockCopy(p, 0, result, 0, len);

Jan 31, 2011 at 9:58 PM

The WriteableBitmapEx library has the ToByteArray and FromArray extension methods.


I also blogged about this a while ago:


- Rene Schulte

Feb 4, 2011 at 2:57 PM

using the Windows Phone 7, i am saving the byte() to SQL Server for using in Silverlight 4

writeablebitmap w = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

i use your conversion to create byte[], but it does not give me a successful byte array..


Feb 4, 2011 at 3:44 PM
What do you mean by "not successful"?
Feb 4, 2011 at 4:27 PM
Edited Feb 4, 2011 at 7:21 PM

fyi, i am in VB, and do not forget, i am using the decoder to obtain my WriteableBitmap, not your function (also cannot find translation function for (int).  also it is difficult to debug the camera, as you cannot run it interpretive mode.
anyway, first my byte.length comes back as about 20mb. i need to convert the byte() back to a WriteableBitmap,
i convert the byte() to a memory stream, BitmapImage.setsource(memorystream) and it errors with
MemoryStream is populated with the 20mb info.

further, i have another function that works, but for some reason the image is clear on the phone side, but fuzzy on the SQL side

dim s as stream = args.ChosenPhoto
dim w as WriteableBitmap = PictureDecoder.DecodeJpeg(s)
dim b as byte() = nothing
Using ms as MemoryStream()
end using

Feb 4, 2011 at 11:23 PM



   As you may have noticed most of the code that I was referring is actually from your blog positing. Just now I downloaded the DLL and used the ToByteArray extension method and have the same results.

   Let me ask my question another way. Is the code below the correct way to convert a System.Windows.Controls.Image to a WriteableBitmap?

 WriteableBitmap writeableBitmap = new WriteableBitmap((int) imgFront.Width, (int) imgFront.Height);
 writeableBitmap.Render(imgFront, new MatrixTransform());

   At a high level my code does the following:

  1. Convert an Image to a WriteableBitmap (as shown above).
  2. Convert WriteableBitmap to a byte[] (using the extension method).
  3. Save the byte[] to a file.

  As the file created as a result of the three steps in not a valid image, I must be doing something wrong in one of the steps.


Dec 19, 2011 at 10:51 AM

This article helps you to capture Silverlight Screen and trasfer encoded byte array to a WCF Service Method.

Hope this helps...

Feb 26, 2014 at 2:25 AM
As for WriteableBitmap to a byte array, you can see this post on image to byte array, which can allow you to convert image in any form, like png, bmp, gif or tiff to byte array.
Mar 5, 2014 at 9:21 AM
Here is another guide to easily convert images to byte array in VB.NET with a few lines only!