C# - 基于计时器截屏
我正在尝试创建一个按设定时间间隔截取屏幕截图的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用实际的文件名进行保存,如下所示:
您的代码正在传递不包含文件名的路径。另外,请检查以确保Environment.GetFolderPath(...) 返回的路径末尾不带“\”,否则路径中最终会出现“\\”。
You need to save with an actual file name, like so:
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.