This project is read-only.

writeablebitmap to jpeg in silverlight

Jan 20, 2011 at 11:45 AM
Edited Jan 20, 2011 at 12:54 PM


I'm trying to convert image from webcam to jpeg (actually i'm trying to make video call by capturing snapshots and send them over the internet, so i need to compress them). i capture image by using captureimageasync method from capturesource class, and get a writeablebitmap as response. image from writeablebitmap is properly shown in rectangle by imagebrush. how can i convert that to jpeg? i've tried next code:

first of all writeablebitmap returns pixels in int array, so we have to convert it to byte array. i found on internet that that can be done like this:


int [] niz = bitmap.Pixels;
int size = niz.GetLength(0);//this is a bit confusing, i 
//have to enter parameter, but i don't know which, 
//so i suggested 0
byte []nizb = new byte[size*sizeof(int)];
for (int i = 0; i < size ; i++)
              int j = 4 * i;
              nizb[j] = (byte)(nizb[i] & 0xFF); 
              nizb[j+1] = (byte)(nizb[i] >> 8 & 0xFF);
              nizb[j+2] = (byte)(nizb[i] >> 16 & 0xFF);
              nizb[j+3] = (byte)(nizb[i] >> 24 & 0xFF);


after that i've tried encoding by this:


BitmapImage b = new BitmapImage();
ExtendedImage ei = new ExtendedImage();
MemoryStream ms = new MemoryStream(nizb);
MemoryStream m = new MemoryStream();
 ei.SetSource( ms);
JpegEncoder je = new JpegEncoder();
je.Encode(ei, m);
and when i try
it throws me an exception. Am i doing something wrong? I'm new to all of this. Please help. Thanks in advance :)


I've tried even with WriteableBitmapEx, but it not works, neither. I can't figure out why setsource is throwing an exception :(

Jan 20, 2011 at 4:09 PM


I would not choose this approach, when you want to do this, you can import the ImageTools.Utils.dll assembly and the ImageTools.IO.Png.dll assembly. The utils lib provides an extension method ToImage();

ExtendedImage image = bitmap.ToImage();

MemoryStream stream = new MemoryStream();

JpegEncoder encoder = new JpegEncoder;
encoder.Encode(image, stream);

But can you please give me details about the exception.

This approach is probably too slow, I would convert the writeablebitmap to an byte array async byte by byte to a transport component and first the width and height of the image, each with 2 bytes.

This transport component should make packages of the byte stream and then using compression to send them by tcp (sockets) to the other computer. This approach is probly the fastest solution.

Jan 20, 2011 at 5:20 PM

exception is "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))" . solution you provided me is working, but it is slow. i made simple project which is displaying video from webcam, and beneath same sized rectangle displaying encoded pictures. the it seems to me that conversion method is taking too much resources causing both video to chop, although i have not so bad laptop configuration (dual core 2Ghz, 3GB ram). is there any faster solution? maybe to change capturing process (Captureimageasync method from capturesource)? it is working in skype, so i know it is possible, but i don't know if similar solution is available for me :) 


ps. thank you very much for answer! :) 

Jan 20, 2011 at 5:23 PM
Mhm, Jpeg Encoding is definitly slower than my suggested approach.
Jan 20, 2011 at 5:33 PM
oh, sorry, i'll try that now :) anyway, thank you very much for your time and your help, really appreciate it :)

2011/1/20 malignate88 <>

From: malignate88

Mhm, Jpeg Encoding is definitly slower than my suggested approach.

Read the full discussion online.

To add a post to this discussion, reply to this email (

To start a new discussion for this project, email

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at

Миладин Радовић