I want to do image processing with real-time image and I’m using C#. I got to grab the image from webcam and it runs smoothly. However, I got some error when I attach with other code for image processing. I cannot run more than one time in one execution debugging.
It gives exception: “Could not grab picture. A generic error occurred in GDI+.”
I just attach one class library to do threshold process for color image that I grabbed.
This is the original class to grab the image:
toolBarBtnGrab.Enabled = true;
int hr;
if( sampGrabber == null )
hr = sampGrabber.SetCallback( null, 0 );
int w = videoInfoHeader.BmiHeader.Width;
int h = videoInfoHeader.BmiHeader.Height;
int stride = w * 3;
GCHandle handle = GCHandle.Alloc( savedArray, GCHandleType.Pinned );
int scan0 = (int) handle.AddrOfPinnedObject();
scan0 += (h - 1) * stride;
Bitmap b = new Bitmap( w, h, -stride, PixelFormat.Format24bppRgb, (IntPtr) scan0 );
savedArray = null;
Image old = pictureBox.Image;
pictureBox.Image = b;

and below is the code that I added after the last line of above code to call class library for threshold the image that was grabbed by above code:
pictureBox.Image.Save("1stImage.Jpeg", ImageFormat.Jpeg);
Bitmap bp = new Bitmap("1stImage.Jpeg");
ClassLibrary1.Class1 filter = new ClassLibrary1.Class1();
System.Drawing.Bitmap binary = filter.Binary(bp);
picClass1.Image = binary;
picClass1.Image.Save("Binary.Jpeg", ImageFormat.Jpeg);
picClass1.Width = binary.Width;
picClass1.Height = binary.Height;
Could any body help me please to overcome this problem. Thanks.

I had a similar problem and from reading other forums it looks like a memory issue (precise I know :) ) And if you try a couple of times it seems to work. Anyway, I fixed it in a really horrible way. Although this fix does work I'm looking for a better one. It's in pseudo I'm afraid

alldone = false;
save the image 
alldone = true
catch(exception e){
//eat it

I've also made a slightly better version which goes

stopdate = now;
stopdate = stopdate.addMins(1);
while(stopdate > now)
save the image
catch(exception e){
check if it the kind of "just need a moment" exception I was expecting
if so
//eat it
if not
throw e;

the date one is great for my problem because i'm grabbing images from a high end digital camera on a filewatcher.filecreated event so sometimes the event fires before the whole image is on the disk.

However after a minute it's time to give up and do something else. Better suggestions welcome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.