|
FYI: when output is line-oriented I prefer a ListBox over a TextBox almost every time.
A ListBox does not need the lines to be concatenated at all, it just shows a list of items.
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]
|
|
|
|
|
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)
|
|
|
|
|
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.
|
|
|
|
|
But what is that going to do? You still have to decode it..
|
|
|
|
|
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[^]
|
|
|
|
|
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.
|
|
|
|
|
Ok it makes more sense now, I hope the OP is reading this as well
|
|
|
|
|
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,
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();
}
}
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]
|
|
|
|
|
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.Maadani
modified 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.
|
|
|
|
|
You're right! my bad. it must be :
System.Drawing.Imaging.ImageFormat.Bmp
I'll correct it now.
|
|
|
|
|
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)
|
|
|
|
|
I'm stepping into a whole new programming world which I'm not used to at the moment and seemingly stuck at the first hurdle.
I have a class called SQLDBConnect.cs This class is given parameters to connect to a database e.g username password, server etc. These are pulled into an SQLConnection and opened. That appears to work fine.
Within another class I am trying to write an INSERT into that database and I'm stuck at the command
SqlCommand myCommand = new SqlCommand();
myCommand.Connection =;
How do I tell the command to use the connection which is opened in the SQLDBConnect class?
As I mentioned I am new to all this and may be taking the wrong approach, I'd appreciate any opinions.
Many Thanks for all help
|
|
|
|
|
You have to open the connection before you can call myCommand.ExecuteXXX . I have a wrapper class that takes care of all that, and I derive new classes from that base class. This base class builds the connection string, opens/closes the connection, and creates the SqlCommand object for each of the various ExecuteXXX methods.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
That's an interesting thought, thank you. Would others agree?
|
|
|
|
|
Yes.
You may like to lok at one of the plethora of Data Access libraries/frameworks available for various takes on the best way to accomplish this well-defined common task.
|
|
|
|
|
Yes, don't re-invent the wheel. There are many very good examples on CodeProject of Data Access Layers (DAL) or you could look at the Microsoft Enterprise Library as this is well supported and documented.
|
|
|
|
|
I would agree that there is no need to reinvent the wheel. The Microsoft Enterprise Library was constructed to provide a library of ready tuse code to solve common programming problems, like data access.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
|
Did you ever notice that the spots on the cow appear to spell out "IE8"?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
It's actually a bull, and I hadn't looked at it that way. Now I'm sure I won't be able not to.
|
|
|
|
|
You could simply do it by hand, creating a new connection is a pretty simple task.
myCommand.Connection = new SqlConnection("...Connection String...")
If you do a quick web search for connection strings for your specific database, you should be able to set that up in no time.
|
|
|
|