very fast image conversion from jpg to bmp in c#
-
Hi, Try to use the same bitmap object instead of creating a new one (I am assuming the images are the same size (resolution wise). Copy the jpeg data to the bitmap but do not use Get/Set pixel methods since they are slow. Instead, pin the image to memory and do the copy there. If that is not fast enough, do it in unmanaged code using C++, it will be much faster.
-
Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance
---------- Eric(M.M)
please tell us more: - what is the declaration of imageBytes? - what code is filling it? - what is the typical size (in pixels) of your images? - what is the typical size (in bytes) of your JPEG files? - how many images do you want to hold (ballpark figure)? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
I tested it and I do not think it is the encoding. Try to do the same with pre loaded bmp files in memory (eliminate the encoding) Here is the sample project I used: http://www.natzamitzi.com/imagetest.zip[^]
-
I tested it and I do not think it is the encoding. Try to do the same with pre loaded bmp files in memory (eliminate the encoding) Here is the sample project I used: http://www.natzamitzi.com/imagetest.zip[^]
-
Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance
---------- Eric(M.M)
Whats wrong with save method of an Image instance!? I think its fast enough! just convert the stream into an Image and then :
Image.Save("filename.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
P.S. : Code is corrected on January 27. thanks to Ennis Ray Lynch, Jr. ------------- Regards H.Maadanimodified on Wednesday, January 27, 2010 11:30 AM
-
What? No I mean, if you just copy the JPG data into a bitmap, it is not decoded and therefore useless
Look at the previous message with the sample code. I conducted a test with jpeg images and it seems like the decoding does not take that much CPU. I suggested that you try to isolate the problem by using a set of predefined images (eliminate the decoding) and see whether the CPU usage continues.
-
Look at the previous message with the sample code. I conducted a test with jpeg images and it seems like the decoding does not take that much CPU. I suggested that you try to isolate the problem by using a set of predefined images (eliminate the decoding) and see whether the CPU usage continues.
-
Whats wrong with save method of an Image instance!? I think its fast enough! just convert the stream into an Image and then :
Image.Save("filename.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
P.S. : Code is corrected on January 27. thanks to Ennis Ray Lynch, Jr. ------------- Regards H.Maadanimodified on Wednesday, January 27, 2010 11:30 AM
Your code would save a jpeg with a file extension of bmp and not save the file as a bitmap.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
please tell us more: - what is the declaration of imageBytes? - what code is filling it? - what is the typical size (in pixels) of your images? - what is the typical size (in bytes) of your JPEG files? - how many images do you want to hold (ballpark figure)? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
thanks for all suggestions and solutions, - imageBytes is exactly like a jpg file that is loaded to the memory (for example using File.ReadAllBytes(), but here the images are coming in from a stream not from files) - The images are 1600x1200 (it seems a little large!) - The images are about 157000 bytes - I have an AMD Athlon 64 X2 Dual Core Processor 6000+ 3.01 GHz 2 GB RAM, converting these jpg frames to bitmap takes approzmately 40% of my dual core cpu (each frame takes about 60ms to be converted to a Bitmap object in bmp format), the image processing algorithms that I apply on the images take too much cpu, but if converting to Bitmap take 20% of cpu, it is ideal for me. - I need at least 10 to 15 frames per second to be converted to Bitmap. BTW, it was my first post in the code project with code, so I forgot to format code lines!
---------- Eric(M.M)
-
Hi all developers, I have spent a lot of time to find a fast way for converting a stream of jpg images to bitmap format in c# (to use in a n image processing unit in my application). The conversion in my code is done as follows: using (MemoryStream ms = new MemoryStream(imageBytes)) { this.bqi.Bitmap = new Bitmap(ms); } in these code lines, imageBytes contains jpg image data ready to convert to bitmap format. These images are coming in from an IP camera as a web server. The frame rate is 15 frames per second, and converting 15 frames per second to Bitmap in this way takes too much time of CPU and the application is almost unable to do any other job when preview is on. Is there any workaround for this conversion without need to much cpu usage? Is there any solution on using directshow for converting jpg to bmp on gpu? any suggestions appreciated. thanks in advance
---------- Eric(M.M)
Any idea on using DirectShow to convert images? because as you know, when you watch a video on your computer, it does not take too cpu time because decoding movie frames is one in gpu, not on cpu. Is there any way on using directshow to convert single frame images? I have researched alot on this idea, but all the things I've found is converting movie from one format to another using directshow. Any suggestions appreciated.
---------- Eric(M.M)
-
Your code would save a jpeg with a file extension of bmp and not save the file as a bitmap.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
thanks for all suggestions and solutions, - imageBytes is exactly like a jpg file that is loaded to the memory (for example using File.ReadAllBytes(), but here the images are coming in from a stream not from files) - The images are 1600x1200 (it seems a little large!) - The images are about 157000 bytes - I have an AMD Athlon 64 X2 Dual Core Processor 6000+ 3.01 GHz 2 GB RAM, converting these jpg frames to bitmap takes approzmately 40% of my dual core cpu (each frame takes about 60ms to be converted to a Bitmap object in bmp format), the image processing algorithms that I apply on the images take too much cpu, but if converting to Bitmap take 20% of cpu, it is ideal for me. - I need at least 10 to 15 frames per second to be converted to Bitmap. BTW, it was my first post in the code project with code, so I forgot to format code lines!
---------- Eric(M.M)
Hi, I ran a couple of experiments, with a set of 100 JPEG images, each larger than 2200*1800 ("original"). I also converted them to a set of 1600*1200 ("medium") and another set of 800*600 ("small"). The test consisted of animating those images to a PictureBox as fast as possible, basically with this code:
foreach (string s in list) { Image oldImage=pb.Image; if (oldImage!=null) oldImage.Dispose(); using (FileStream stream=File.Open(s, FileMode.Open)) { Bitmap bm=new Bitmap(stream); pb.Image=bm; Application.DoEvents(); // update the PictureBox } }
And the time required on a laptop (with an Intel 2.4GHz Core2 Duo) was 18/8/2 seconds for original/medium/small images, telling me the image size (in pixels) is of utmost importance. So, if at all possible, I recommend you tell your camera or webcam to output lower-resolution images. BTW: the same experiment with bitmap images (*.BMP) is terribly slow, due to the size of the files involved. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]