c#截取后台窗口的图片
2016-03-28 09:46
806 查看
c#截取后台窗口的图片,自测可用,据说性能很一般,用用吧
struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } public static Bitmap GetWindow(IntPtr hWnd) { IntPtr hscrdc = GetWindowDC(hWnd); RECT rect = new RECT(); GetWindowRect(hWnd, out rect); IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, rect.Right - rect.Left, rect.Bottom - rect.Top); IntPtr hmemdc = CreateCompatibleDC(hscrdc); SelectObject(hmemdc, hbitmap); PrintWindow(hWnd, hmemdc, 0); Bitmap bmp = Bitmap.FromHbitmap(hbitmap); DeleteDC(hscrdc); DeleteDC(hmemdc); return bmp; } //API声明 [DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("gdi32.dll")] public static extern IntPtr CreateDC( string lpszDriver, // driver name驱动名 string lpszDevice, // device name设备名 string lpszOutput, // not used; should be NULL IntPtr lpInitData // optional printer data ); [DllImport("gdi32.dll")] public static extern int BitBlt( IntPtr hdcDest, // handle to destination DC目标设备的句柄 int nXDest, // x-coord of destination upper-left corner目标对象的左上角的X坐标 int nYDest, // y-coord of destination upper-left corner目标对象的左上角的Y坐标 int nWidth, // width of destination rectangle目标对象的矩形宽度 int nHeight, // height of destination rectangle目标对象的矩形长度 IntPtr hdcSrc, // handle to source DC源设备的句柄 int nXSrc, // x-coordinate of source upper-left corner源对象的左上角的X坐标 int nYSrc, // y-coordinate of source upper-left corner源对象的左上角的Y坐标 UInt32 dwRop // raster operation code光栅的操作值 ); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC( IntPtr hdc // handle to DC ); [DllImport("gdi32.dll")] public 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")] public static extern IntPtr SelectObject( IntPtr hdc, // handle to DC IntPtr hgdiobj // handle to object ); [DllImport("gdi32.dll")] public static extern int DeleteDC( IntPtr hdc // handle to DC ); [DllImport("user32.dll")] public 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")] public static extern IntPtr GetWindowDC( IntPtr hwnd );
相关文章推荐
- C#利用GDI绘制常见图形和文字
- win2008下c#调用directshow问题
- C# GDI三种坐标系及颜色等常用结构
- 初识google多语言通信框架gRPC系列(三)C#中使用gRPC
- C# 已知圆心和两点,用DrawArc()画圆弧(算法)
- 【C#进阶系列】17 委托
- 【C#进阶系列】16 委托
- c# 科学计算讲义
- 使用C#模拟简易的计算器
- C#多线程处理
- C#学习-多线程小练习
- c#泛型优点
- VS C#中,与config文件的操作有关的问题
- 我的C# GitHub仓库建立
- C# 类中索引器的使用
- C#组态控件Iocomp应用案例
- 改善C#程序的建议3:在C#中选择正确的集合进行编码
- c#DataGridView数据绑定示例——格式化单元格的内容(转)
- C#读取文件 中文显示问号
- C#版的组合查询+模板方法