C# 获取非前端窗口的截图

截取截图

参数为被截图窗口的窗口句柄

        #region GetWindowCapture的dll引用
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleDC(
         IntPtr hdc // handle to DC
         );
        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(
         IntPtr hdc,         // handle to DC
         int nWidth,      // width of bitmap, in pixels
         int nHeight      // height of bitmap, in pixels
         );
        [DllImport("gdi32.dll")]
        private static extern IntPtr SelectObject(
         IntPtr hdc,           // handle to DC
         IntPtr hgdiobj    // handle to object
         );
        [DllImport("gdi32.dll")]
        private static extern int DeleteDC(
         IntPtr hdc           // handle to DC
         );
        [DllImport("user32.dll")]
        private static extern bool PrintWindow(
         IntPtr hwnd,                // Window to copy,Handle to the window that will be copied.
         IntPtr hdcBlt,              // HDC to print into,Handle to the device context.
         UInt32 nFlags               // Optional flags,Specifies the drawing options. It can be one of the following values.
         );
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(
         IntPtr hwnd
         );
        #endregion

        public static Bitmap GetWindowCapture(IntPtr hWnd)
        {
            IntPtr hscrdc = GetWindowDC(hWnd);
            Rectangle windowRect = new Rectangle();
            GetWindowRect(hWnd, ref windowRect);
            int width = Math.Abs(windowRect.X - windowRect.Width);
            int height = Math.Abs(windowRect.Y - windowRect.Height);
            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, hbitmap);
            PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Image.FromHbitmap(hbitmap);
            DeleteDC(hscrdc);//删除用过的对象
            DeleteDC(hmemdc);//删除用过的对象
            return bmp;
        }

原帖-CSDN-thomas_02-C# 非顶端窗口截图 - 用于查找指定窗口并截图


获得任意窗口的句柄

需要调用到FindWindow函数,参数一是欲获取窗口的类名,参数二是欲获取窗口的窗口名,可以只提供一个,另一个给null,成功返回窗口句柄,失败返回0

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

利用SPY++获得窗口的类名和窗口名

在vs顶部的菜单栏中,选择 工具-SPY++
这里写图片描述

CTRL+F 打开查找窗口,把中间的东东拉出去拉到你要查的窗口上
这里写图片描述

由此可知图中的类名是“VirtuaNESwndclass”,编写代码得到句柄

            IntPtr intPtr = FindWindow("VirtuaNESwndclass", null);

参考资料
博客园-beautifulzzzz-[WinAPI] 获取窗口句柄的几种方法
博客园-Net-Spider-C# FindWindow用法

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C#中的System.Drawing.Bitmap类和Win32 API来获取窗口截图。具体的代码如下: ```csharp using System; using System.Drawing; using System.Runtime.InteropServices; public class WindowCapture { [DllImport("User32.dll")] public static extern IntPtr GetDC(IntPtr hwnd); [DllImport("User32.dll")] public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleBitmap(IntPtr dc, int width, int height); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr dc); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr dc, IntPtr obj); [DllImport("gdi32.dll")] public static extern bool BitBlt(IntPtr dcDest, int xDest, int yDest, int wDest, int hDest, IntPtr dcSrc, int xSrc, int ySrc, int rasterOp); public static Bitmap CaptureWindow(IntPtr hwnd) { // 获取窗口的设备上下文 IntPtr hdcSrc = GetDC(hwnd); if (hdcSrc == IntPtr.Zero) { return null; } // 创建一个兼容的设备上下文和位图 IntPtr hdcDest = CreateCompatibleDC(hdcSrc); IntPtr hbmDest = CreateCompatibleBitmap(hdcSrc, 100, 100); if (hdcDest == IntPtr.Zero || hbmDest == IntPtr.Zero) { ReleaseDC(hwnd, hdcSrc); return null; } // 将位图选入设备上下文 IntPtr hbmOld = SelectObject(hdcDest, hbmDest); // 复制窗口的内容到位图中 if (!BitBlt(hdcDest, 0, 0, 100, 100, hdcSrc, 0, 0, 0x00CC0020)) { SelectObject(hdcDest, hbmOld); DeleteObject(hbmDest); DeleteDC(hdcDest); ReleaseDC(hwnd, hdcSrc); return null; } // 恢复设备上下文并释放资源 SelectObject(hdcDest, hbmOld); DeleteObject(hbmDest); DeleteDC(hdcDest); ReleaseDC(hwnd, hdcSrc); // 返回位图 return Bitmap.FromHbitmap(hbmDest); } } ``` 使用方法: ```csharp IntPtr hwnd = // 窗口句柄 Bitmap bmp = WindowCapture.CaptureWindow(hwnd); ``` 其中,hwnd为窗口句柄,bmp为获取到的窗口截图。请注意,此代码仅支持获取窗口的静态截图,无法实时获取窗口的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值