Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear someone:

Now I am trying to do some "object Tracing" by using my web-camera. I read some paper said that I need to catch one picture to be the background and use another picture to find out different part to circular it out.
But when I compiled my program, it said that GDI+ error....I don't know it's my logical part incorrect or what should I need to change.

C#
private void timer2_Tick(object sender, EventArgs e)
        {
            Bitmap myBitmap1 = pictureBox1.Image as Bitmap;
            if (myBitmap1 == null)
            {
                timer2.Stop();
            }
            else
            {
                // Here to Get BackGround Image and save to disk
                int[, ,] ImgData1 = GetImgData(myBitmap1);
                GrayProcess(ImgData1);
                GrayThreshold(ImgData1);
                Bitmap ProcessedBitmap1 = CreateBitmap(ImgData1);
                ProcessedBitmap1.Save(@"c:\temp1.jpg");

                delay(1);
                
                // Here to Get another Image and save to disk
                int[, ,] ImgData2 = GetImgData(myBitmap1);
                GrayProcess(ImgData2);
                GrayThreshold(ImgData2);
                Bitmap ProcessedBitmap2 = CreateBitmap(ImgData2);
                ProcessedBitmap2.Save(@"c:\temp2.jpg");
            }

            // Here I try to calculate changed pixel and compare both pictures to find out location
            Bitmap source1 = new Bitmap(@"c:\temp1.jpg");
            Bitmap source2 = new Bitmap(@"c:\temp2.jpg");

            int[, ,] New1 = new int[source1.Width, source1.Height, 3];
            int[, ,] New2 = new int[source2.Width, source2.Height, 3];
            BitmapData byteArray1 = source1.LockBits(new Rectangle(0, 0, source1.Width, source1.Height), 

ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData byteArray2 = source2.LockBits(new Rectangle(0, 0, source2.Width, source2.Height), 

ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            int ByteOfSkip1 = byteArray1.Stride - byteArray1.Width * 3;
            int ByteOfSkip2 = byteArray2.Stride - byteArray2.Width * 3;
            unsafe
            {
                byte* imgPtr1 = (byte*)(byteArray1.Scan0);
                byte* imgPtr2 = (byte*)(byteArray2.Scan0);
                for (int y = 0; y <byteArray1.Height; y++)
                {
                    for (int x = 0; x < byteArray1.Width; x++)
                    {
                        New1[x, y, 2] = (int)*(imgPtr1);
                        New1[x, y, 1] = (int)*(imgPtr1+1);
                        New1[x, y, 0] = (int)*(imgPtr1+2);
                        New2[x, y, 2] = (int)*(imgPtr2);
                        New2[x, y, 1] = (int)*(imgPtr2 + 1);
                        New2[x, y, 0] = (int)*(imgPtr2 + 2);
                        imgPtr1 += 3;
                        imgPtr2 += 3;

                        int a = New1[x, y, 1];
                        int b = New2[x, y, 1];

                        if (a != b)
                        {

                            //Here to identify the location and circular it out
                            Graphics g = pictureBox1.CreateGraphics();
                            Pen pen = new Pen(Color.Red, 5);
                            int width, height,width1,height1;
                            width = source1.Width;
                            height = source1.Height;
                            width1 = pictureBox1.Size.Width;
                            height1 = pictureBox1.Size.Height;
                            int width_diff, height_diff;
                            width_diff = width1 - width;
                            height_diff = height1 - height;
                            g.DrawRectangle(pen, x, y, width_diff, height_diff);
                            //Refresh();
                        }

                    }
                    imgPtr1 += ByteOfSkip;
                    imgPtr2 += ByteOfSkip;
                }
            }
            source1.UnlockBits(byteArray1);
            source2.UnlockBits(byteArray2);
        }
Posted
Updated 31-Jan-21 21:26pm
v5

1 solution

No.
The Tick event handler is a void method - it cannot return any value (it isn't called directly from your code, so there is nowhere to return the value to)

And the Tick even handler does not take an integer parameter either, so you can't do that either!
Instead, create a class level private variable which can transfer the information between the two Tick handlers.

But be aware that Tick event handler may not be called as precisely as you expect: they are not meant for true real-time processing.

What are you trying to achieve, that you think this is a good idea (or even necessary) - there may be a better way to handle the whole thing.
 
Share this answer
 

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