1
votes

So I have 2 classes: ImageScanner and Form1

What I want to do is keep calling the ScreenCapture method in my while loop until a certain condition is met.

This Method captures my screen and saves the screenshot as a PNG file in the same place. This means every time I take a screenshot, the new one replaces the old one.

I am trying to do this when I click a button on my form so I don't know why C# is giving me an error.

I can however call the ScreenCapture method when it is not in my loop.

class ImageScanner
    {
        public static void ScreenCapture()
        {
            var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                                           Screen.PrimaryScreen.Bounds.Height,
                                           PixelFormat.Format32bppPArgb);

            // Create a graphics object from the bitmap.
            var gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            // Take the screenshot from the upper left corner to the right bottom corner.
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                                        Screen.PrimaryScreen.Bounds.Y,
                                        0,
                                        0,
                                        Screen.PrimaryScreen.Bounds.Size,
                                        CopyPixelOperation.SourceCopy);

            // Save the screenshot to the specified path that the user has chosen.
            bmpScreenshot.Save("Screenshot.png", ImageFormat.Png);
        }
    }



public partial class ClickForm : Form
{
    private void ECFile_Click(object sender, EventArgs e)
    {
        int j = 0;

        while (j < 20)
        {
            j += 1;
            ImageScanner.ScreenCapture();
        }
        MessageBox.Show("found");
    }
}

Error Message:

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at AutoClicker.ImageScanner.ScreenCapture() in C:\Users\User1\source\repos\AutoClicker\AutoClicker\ImageScanner.cs:line 32 at AutoClicker.ClickForm.ECFile_Click(Object sender, EventArgs e) in C:\Users\User1\source\repos\AutoClicker\AutoClicker\Form1.cs:line 232 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

2
" I don't know why C# is giving me an error." - What error? Please provide more detailsDai
"unhandled exception has occurred in your application"jun
What is the exception?DCCoder
Added the exceptionjun
Did you try to debug th check which line in the code throws the exception?Chetan

2 Answers

3
votes

This is generally happening due to permission limitation, give enough permission to the folder you save to

As you can see it is stuck in

System.Drawing.Image.Save

the good practice to start with giving Everyone full control, then after test and pass try to reduce the permission.

When saving a file, you should provide a full path including the Directory, folder, file, and file extension. Example: C:\abcd\filename.bmp

Out of subject but good to mention: Also, consider you are dealing with external resources so there will be a delay of input and output, it is recommended to put after saving command Thread.Delay(someseconds) as well, especially, if you save and use the saved file by another code directly (like dealing with temp files).

0
votes

Try Converting the image to stream before saving. Refer link below.

Image image;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            image = Image.FromStream(ms);
        }

        image.Save(...);