views:

212

answers:

2

Is there a way that I can use the BitmapFrame returned from JpegBitmapDecoder in the UserControl.OnPaint() method? I was told that the performance of JPEG decoding from Systems.Windows.Media.Imaging is much better than the one in GDI+ used by Systems.Windows.Forms library. However, my application is written with Systems.Windows.Forms library already and I don't want to change everything. All I need is a faster way to decompress the JPEG frame and draw it in OnPaint() method.

+1  A: 

I figured out the answer myself. Here are the sample code:

JpegBitmapDecoder decoder = new JpegBitmapDecoder(pixelStream, BitmapCreateOptions.None, BitmapCacheOption.None);
BitmapFrame frame = decoder.Frames[0];
frame.CopyPixels(pixelBuffer, stride, 0);

pixelBuffer is a preallocated byte array. And then I can use it to construct a Bitmap use in OnPaint().

JohnY
A: 

If your answer is sufficient will you please accept it so the question will get closed?

In any case I was curious if you were then using a Graphics.DrawImage() to paint your Bitmap? I was recently working with Graphics.DrawImage() and found it to be pretty slow. Wondering if your pre-decoding actually sped up the process.

Cory Charlton
I do use Graphics.DrawImage(). I did not measure the difference before and after my change. DrawImage() performance is affected by lots of other things such as Graphics.InterpolationMode, the size of your source image and drawing size, etc.
JohnY