Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a byte[] from a BitmapImage in Silverlight?

I'm trying to pass some representation of an image back and forth between Silverlight and a WCF service. If possible I'd like to pass a System.Windows.Media.Imaging.BitmapImage, since that would mean the client doesn't have to do any conversion.

However, at some point I need to store this image in a database, meaning the image representation must be able to convert to and from byte[]. I can create a BitmapImage from a byte[] by reading the array into a MemoryStream and using BitmapImage.SetSource(). But I can't seem to find a way to convert the other way - from BitmapImage to byte[]. Am I missing something obvious here?

If it helps at all, the conversion code could run on the server, i.e. it doesn't need to be Silverlight-safe.

like image 504
goric Avatar asked Dec 05 '09 02:12

goric


People also ask

How do I create a byte array image?

To convert a byte array to an image. Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter).

How do I convert a bitmap to a byte?

In addition, you can simply convert byte array to Bitmap . var bmp = new Bitmap(new MemoryStream(imgByte)); You can also get Bitmap from file Path directly. Save this answer.

What is a Bytearray?

A consecutive sequence of variables of the data type byte, in computer programming, is known as a byte array. An array is one of the most basic data structures, and a byte is the smallest standard scalar type in most programming languages.


2 Answers

Use this:

public byte[] GetBytes(BitmapImage bi)
{
    WriteableBitmap wbm = new WriteableBitmap(bi);
    return wbm.ToByteArray();
}

Where

public static byte[] ToByteArray(this WriteableBitmap bmp)
{
    // Init buffer
    int w = bmp.PixelWidth;
    int h = bmp.PixelHeight;
    int[] p = bmp.Pixels;
    int len = p.Length;
    byte[] result = new byte[4 * w * h];

    // Copy pixels to buffer
    for (int i = 0, j = 0; i < len; i++, j += 4)
    {
        int color = p[i];
        result[j + 0] = (byte)(color >> 24); // A
        result[j + 1] = (byte)(color >> 16); // R
        result[j + 2] = (byte)(color >> 8);  // G
        result[j + 3] = (byte)(color);       // B
    }

    return result;
}
like image 143
Vladimir Dorokhov Avatar answered Oct 04 '22 03:10

Vladimir Dorokhov


I had the same issue. I found the ImageTools library that makes thte job way easier.

Get the library and reference it and then

                        using (var writingStream = new MemoryStream())
                        {
                            var encoder = new PngEncoder
                            {
                                IsWritingUncompressed = false
                            };
                            encoder.Encode(bitmapImageInstance, writingStream);
                            // do something with the array
                        }
like image 24
R4cOOn Avatar answered Oct 04 '22 03:10

R4cOOn