获取特定应用程序的屏幕截图 [英] Get a screenshot of a specific application
本文介绍了获取特定应用程序的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以使用 Graphics.CopyFromScreen() 获取整个屏幕的屏幕截图.但是,如果我只想要特定应用程序的屏幕截图怎么办?
I know I can get the screenshot of the entire screen using Graphics.CopyFromScreen(). However, what if I just want the screenshot of a specific application?
推荐答案
以下是一些帮助您入门的代码:
Here's some code to get you started:
public void CaptureApplication(string procName)
{
var proc = Process.GetProcessesByName(procName)[0];
var rect = new User32.Rect();
User32.GetWindowRect(proc.MainWindowHandle, ref rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bmp))
{
graphics.CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
}
bmp.Save("c:\tmp\test.png", ImageFormat.Png);
}
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
}
它有效,但需要改进:
- 您可能希望使用不同的机制来获取进程句柄(或至少进行一些防御性编码)
- 如果您的目标窗口不在前景中,您最终会得到一个大小/位置正确的屏幕截图,但只会填充前景中的任何内容(您可能想将给定的窗口拉入前景优先)
- 您可能想要做的不仅仅是将 bmp 保存到临时目录
这篇关于获取特定应用程序的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文