gpt4 book ai didi

C# - 基于定时器截屏

转载 作者:行者123 更新时间:2023-11-30 12:18:15 24 4
gpt4 key购买 nike

我正在尝试创建一个 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);

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

最佳答案

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

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

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

关于C# - 基于定时器截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2549624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com