1

can any one has idea. how to convert BitmapImage to Byte[] in silverlight4 application i have one image file in .xap file.

BitmapImage bi = new BitmapImage(new Uri("images/GRed.png", UriKind.Relative));

now i want to convert BitmapImage to Byte[] and save in to db as binary format.

Chirag
  • 375
  • 4
  • 30

1 Answers1

1
    private byte[] ToByteArray(BitmapImage bi)
    {
        WriteableBitmap bmp = new WriteableBitmap(bi);
        int[] p = bmp.Pixels;
        int len = p.Length * 4;
        byte[] result = new byte[len]; 
        Buffer.BlockCopy(p, 0, result, 0, len);
        return result;
    }
tsiorn
  • 2,236
  • 1
  • 22
  • 26
  • WriteableBitmap bmp = new WriteableBitmap(bi); at this line it's give me a error **Object reference not set to an instance of an object.** – Chirag Mar 09 '12 at 07:07
  • Is it possible the BitmapImage you are passing is null? Can you debug and make sure your code for reading in the image is not returning null? – tsiorn Mar 12 '12 at 15:48