c# Capture image from screen and get colors : I'm making a program which can capture a small area on screen and will run something if there is any color on image that match the target colors. My program run as the following Sequence:

1/ Get image from a specific area from screen

2/ Save to a folder

3/ using CountPixel to detect any target_color
However, it through an exception at ""b.Save(@"C:\Applications\CaptureImage000.jpg", ImageFormat.Jpeg);"" after I click the "button5" twice times (not double click). Additional information: A generic error occurred in GDI+.

My questions are:

1. How can i fix this exception.
2. I want to use another method instead of CountPixel() to improve performance, because I just need to detect only one target color to rise event.
3. Step 2 is troublesome. I wonder if i can skip it and use the other way to call: (@"C:\Applications\CaptureImage000.jpg", ImageFormat.Jpeg) , because using this long string isn't comfortable and result error when im trying to use with GetPixel,... or add it into some "value example" code on internet for improvement purpose.



        private int CountPixels(Bitmap bm, Color target_color)
        {
            // Loop through the pixels.
            int matches = 0;
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    if (bm.GetPixel(x, y) == target_color) matches++;
                }
            }
            return matches;
        }
        private Bitmap CapturedImage(int x, int y)
        {
            Bitmap b = new Bitmap(XX, YY);


            Graphics g = Graphics.FromImage(b);
            g.CopyFromScreen(x, y, 0, 0, new Size(XX, YY));

            b.Save(@"C:\Applications\CaptureImage000.jpg", ImageFormat.Jpeg);

            /* Run 3 line below will lead to question 1 - through exception
            Bitmap bm = new Bitmap(@"C:\Applications\CaptureImage000.jpg");
            int black_pixels = CountPixels(b, Color.FromArgb(255, 0, 0, 0));
            textBox3.Text = black_pixels + " black pixels";
            */

            return b;
        }
        private void button5_Click(object sender, EventArgs e)// Do screen cap
        {
            Bitmap bmp = null;
            bmp = CapturedImage(X0, Y0);
        }
}