views:

1197

answers:

2

In a .NET CF application I wrote, one of the features is to acquire frames from remote cameras. Frames are acquired as single jpeg images and displayed on the screen when available.

It was a good enough solution, but I don't like the fact that the time needed to convert the stream into an Image object, with the Bitmap() constructor, is far far far larger than the time needed to download the stream.

When I surfed some blogs to search about this issue, I found that some developers were using the Image.FromStream() method which has a validateImageData flag that seems to control some validation code. When validateImageData is false, the conversion gets dramatically faster.

Good, I thought .... but the Compact Framework does not implement this method !

Anyone knows how to get around it, or at least how to convert a stream into an Image without unnecessary delays ?

+1  A: 

If the dimensions of the JPEG are large (like 1200 x 1600 or something), then one of the speed problems you're having on your device with the Bitmap constructor is the size of the Bitmap is has to create in memory (the bitmap would be 1200 x 1600 also, even if it's then displayed on a 240 X 320 screen). If you can retrieve an already-sized JPEG of 320 X 240 instead of a full-sized one, the bitmap creation time should be much less.

I couldn't find any alternatives to the missing FromStream method other than the Bitmap constructor. If you're familiar with the JPEG format, I don't think it would be too hard to write your own JPEG-to-BMP converter, but I'd guess that it wouldn't be faster than Bitmap().

Edit: sorry, I meant your own-rolled converter wouldn't be any faster at converting a 240 X 320 JPEG into a 240 X 320 bitmap. I think it could be made a lot faster at converting a much larger JPEG into a 240 X 320 bitmap because it could skip the step of creating the full-size bitmap first.

MusiGenesis
Thank you, but the image size is small (320x240) but the performance in the conversion is still very slow (in proportion).
egapotz
+1  A: 

Not sure if this will help but OpenNETCF.Drawing.Imaging has classes to read images from streams, but they are IImage wrapped com objects. You can then use IImage.Draw to draw it onto your bitmap. It also has a GetThumbnail method that I use to get a scaled image of large jpegs on the device.

Here is a blog post about it from Alex Feinman.

Matt
I'm not a fan of OpenNETCF but I think I'll give it a try. Thank you Matt.
egapotz
Well the classes that you would be interested in are wrappers around some com objects, so you could roll your own if you wanted to.
Matt