C# 如何在 while 循环中从另一个 class 调用 void 方法

C# how to call a void method from another class in a while loop

所以我有 2 类:ImageScannerForm1

我想做的是在我的 while 循环中不断调用 ScreenCapture 方法,直到满足特定条件。

此方法捕获我的屏幕并将屏幕截图保存为 PNG 文件在同一位置。这意味着每次我截屏时,新的都会替换旧的。

我在单击表单上的按钮时尝试执行此操作,所以我不知道为什么 C# 会给我一个错误。

然而,当 ScreenCapture 方法不在我的循环中时,我可以调用它。

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");
    }
}

错误信息:

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)

这通常是由于权限限制导致的,请对您保存到的文件夹给予足够的权限

如您所见,它卡在了

System.Drawing.Image.Save

最好的做法是先给每个人完全控制权,然后在测试并通过后尝试减少权限。

保存文件时,您应该提供完整路径,包括目录、文件夹、文件和文件扩展名。示例:C:\abcd\filename.bmp

题外话但值得一提:另外,考虑到你是处理外部资源所以输入输出会有延迟,建议保存后再放命令 Thread.Delay(someseconds) 也一样,特别是,如果您直接通过另一个代码保存和使用保存的文件(比如处理临时文件)。

尝试在保存前将图像转换为流。请参阅下面的 link。

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

        image.Save(...);