C# - 基于计时器截屏

发布于 2024-08-27 12:30:56 字数 1530 浏览 4 评论 0原文

我正在尝试创建一个按设定时间间隔截取屏幕截图的 WinForms 应用程序。我认为我的代码是正确的,但是当我尝试运行它时,我收到错误消息“System.Runtime.InteropServices.ExternalException 未处理,GDI+ 中发生一般错误。”

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    Thread th;
    private static Bitmap bmpScreenshot;
    private static Graphics gfxScreenshot;

    void TakeScreenShot()
    {
        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
        gfxScreenshot = Graphics.FromImage(bmpScreenshot);
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        bmpScreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ScreenCaptures", ImageFormat.Png);
        th.Abort();
    }

    void StartThread(object sender, EventArgs e)
    {
        th = new Thread(new ThreadStart(TakeScreenShot));
        th.Start();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ScreenCaptures");
        t.Interval = 500;
        t.Tick += new EventHandler(StartThread);
        t.Start();
    }

给我带来麻烦的是:

bmpScreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ScreenCaptures", ImageFormat.Png);

对出了什么问题有什么想法吗?提前致谢。

I'm trying to create a WinForms app that takes a screenshot on a set interval. I think my code is correct, but when I try to run it, I get the error message "System.Runtime.InteropServices.ExternalException was unhandled, A generic error occurred in GDI+."

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    Thread th;
    private static Bitmap bmpScreenshot;
    private static Graphics gfxScreenshot;

    void TakeScreenShot()
    {
        bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
        gfxScreenshot = Graphics.FromImage(bmpScreenshot);
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        bmpScreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ScreenCaptures", ImageFormat.Png);
        th.Abort();
    }

    void StartThread(object sender, EventArgs e)
    {
        th = new Thread(new ThreadStart(TakeScreenShot));
        th.Start();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ScreenCaptures");
        t.Interval = 500;
        t.Tick += new EventHandler(StartThread);
        t.Start();
    }

The line that's giving my trouble is:

bmpScreenshot.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ScreenCaptures", ImageFormat.Png);

Any ideas about what is going wrong? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2024-09-03 12:30:57

您需要使用实际的文件名进行保存,如下所示:

bmpScreenshot.Save(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory) 
    + @"\ScreenCaptures\newfile.png", ImageFormat.Png);

您的代码正在传递不包含文件名的路径。另外,请检查以确保Environment.GetFolderPath(...) 返回的路径末尾不带“\”,否则路径中最终会出现“\\”。

You need to save with an actual file name, like so:

bmpScreenshot.Save(Environment.GetFolderPath
    (Environment.SpecialFolder.DesktopDirectory) 
    + @"\ScreenCaptures\newfile.png", ImageFormat.Png);

Your code is passing in a path that does not include a file name. Also, check to make sure that Environment.GetFolderPath(...) returns a path without a "\" on the end, or you'll end up with a "\\" in your path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文