Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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);
            FinalVideoSource.Start();
        }

        void FinalVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                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
                attributes.SetColorMatrix(colorMatrix);
                //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;
                g.Dispose();
                attributes.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }


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
Updated 25-Feb-13 3:34am
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900