获取特定应用程序的屏幕截图

获取特定应用程序的屏幕截图,第1张

获取特定应用程序的屏幕截图

以下是一些入门代码:

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);}

可以,但是需要改进:

  • 您可能希望使用其他机制来获取进程句柄(或至少进行一些防御性编码)
  • 如果您的目标窗口不在前景中,您将得到截屏,该屏幕截图的大小/位置正确,但是将被前景中的所有内容填充(您可能希望先将给定的窗口拉入前景中) )
  • 除了将bmp保存到临时目录外,您可能还想做其他事情


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4935668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存