【发布时间】:2011-01-14 15:07:27
【问题描述】:
我正在编写一个简单的 Windows 窗体实用程序来截取整个屏幕或活动窗口并保存文件。 winform 有一个名为“Capture”的按钮,单击该按钮会截取屏幕截图。
我的代码完全适用于整个屏幕,但无法为活动窗口弄清楚。
根据我当前的代码,如果我选择“活动窗口”,它最终会打印筛选应用程序本身,这不是我想要的。
这是我的代码(在 Capture_Click 方法下):
try
{
this.Hide();
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
//bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
if (AltPrint.Checked)
{
gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);
}
else
{
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
}
bmpScreenshot.Save(SaveLocation, ImageFormat.Png);
number++;
}
【问题讨论】:
-
你在重建
PrintScreen和Alt+PrintScreenkeypresses吗? -
select active window到底是什么意思?你是怎么选的? -
@Austin:不,我的 winform 有一个名为 Capture 的按钮。点击它,截图。上面粘贴的代码在button_Click方法中。
-
@rmx:通过单击窗口标题栏进行选择。我确实意识到,一旦我在我的 winform 上单击“捕获”,之前的窗口就不再被选中,所以我正在寻找一种可能的解决方法。
-
xbonez 请参阅我的回答,了解如何做到这一点。您需要了解一些关于 p/invoke 的知识。