Click here to Skip to main content
13,143,708 members (31,064 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Sir,

I am developing a image processing project in which captured image is first converted into GrayScale after that processing on image with color according to dark spot.

First of all i am converting captured image into grayscale where I wrote following code and got an error.

private void cmb_cameras_SelectedIndexChanged(object sender, EventArgs e)
            //selected video source
            FinalVideoSource = new VideoCaptureDevice(VideoCaptureDevices[cmb_cameras.SelectedIndex].MonikerString);
            FinalVideoSource.NewFrame += new NewFrameEventHandler(FinalVideoSource_NewFrame);
        void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
                LiveImage = (Bitmap)eventArgs.Frame.Clone();
                Bitmap newBitmap = new Bitmap(LiveImage.Width, LiveImage.Height);
                //create a blank bitmap the same size as original
                //get a graphics object from the new image
                Graphics g = Graphics.FromImage(newBitmap);
                //create the grayscale ColorMatrix
                ColorMatrix colorMatrix = new ColorMatrix(
                   new float[][]
                 new float[] {.3f, .3f, .3f, 0, 0},
                 new float[] {.59f, .59f, .59f, 0, 0},
                 new float[] {.11f, .11f, .11f, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {0, 0, 0, 0, 1}
                //create some image attributes
                ImageAttributes attributes = new ImageAttributes();
                //set the color matrix attribute
                //draw the original image on the new image
                //using the grayscale color matrix
                g.DrawImage(LiveImage, new Rectangle(0, 0, LiveImage.Width, LiveImage.Height),
                   0, 0, LiveImage.Width, LiveImage.Height, GraphicsUnit.Pixel, attributes);
                PB_LiveImage.Image = newBitmap;
            catch (Exception ex)

I am using Aforge to captured image.

The error I face:

System.ArgumentException: Parameter is not valid.
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at FootPrintMapping.MainScreen.FinalVideoSource_NewFrame(Object sender, NewFrameEventArgs eventArgs) in MainScreen.cs:line 59
Posted 25-Feb-13 0:49am
Updated 25-Feb-13 3:34am
Chris Reynolds (UK) 25-Feb-13 7:16am
I'd guess that there is something wrong with the bitmap coming in on the event args. Can you check what Width and Height are of the passed in image - you may need to sanity check them.

LiveImage = (Bitmap)eventArgs.Frame.Clone();
Bitmap newBitmap = new Bitmap(LiveImage.Width, LiveImage.Height);
rajesh@1989 25-Feb-13 7:28am
Thank you!!
width:640, height:480
I am missing one point in my posted question that this error is occurred after successfully converted and display in picturebox for few minutes.
Pascal-78 25-Feb-13 9:51am
If the problem occurs after few minutes, you may have a resource problem. You should Dispose the old image of your PictureBox each time you change it.
Garbage Collector will free some object when the memory resource is low, but not when the GDI resource is low (Bitmaps are stored as GDI resources until you dispose them)
rajesh@1989 26-Feb-13 2:05am
Thank you!!
I have resolved this issue by initialize bitmap on form_load like this:
private void MainScreen_Load(object sender, EventArgs e)
NewBitmap = new Bitmap(100, 100);
Memory once allocated to NewBitmap and can used it every time.
rajesh@1989 26-Feb-13 2:09am
Now, I am facing one another problem that I want to convert GrayScale image into Red, Yellow and Green according to dark spot found on GrayScale image.

Is this possible through that?

Highly appreciate!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100