获得特定的应用程序的屏幕截图 [英] 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?
推荐答案
下面是一些code,让你开始:
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);
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);
}
它的工作原理,但需要改进:
It works, but needs improvement:
- 您可能需要使用不同的机制来获得进程句柄(或至少做一些防御性编码)
- 如果您的目标窗口不是在前台,你会最终有一个截图这是正确的大小/位置,而只是充满无论是在前台(你可能想给定的窗口拉进前台第一)
- 您可能想要做其他的东西不仅仅是保存BMP到一个临时目录
这篇关于获得特定的应用程序的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文