不会被其他窗体遮挡的抓图 使用API的PrintWindow函数

2016-01-29 13:05 194 1 收藏

不会被其他窗体遮挡的抓图 使用API的PrintWindow函数,不会被其他窗体遮挡的抓图,使用API的PrintWindow函数

【 tulaoshi.com - ASP.NET 】


只要窗体的visable为true,即使它在屏幕的外面也可以抓到图。如果为false,就是一张黑图了,赫赫。

public static Bitmap GetWindow(IntPtr hWnd)
{
IntPtr hscrdc = GetWindowDC(hWnd);
Control control = Control.FromHandle(hWnd);
IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, control.Width, control.Height);
IntPtr hmemdc = CreateCompatibleDC(hscrdc);
SelectObject(hmemdc, hbitmap);
PrintWindow(hWnd, hmemdc, 0);
Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
DeleteDC(hscrdc);//删除用过的对象
DeleteDC(hmemdc);//删除用过的对象
return bmp;
}

API声明

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

来源:https://www.tulaoshi.com/n/20160129/1488822.html

延伸阅读
WNetAddConnection创建同一个网络资源的永久性连接WNetAddConnection2创建同一个网络资源的连接WNetAddConnection3创建同一个网络资源的连接WNetCancelConnection结束一个网络连接WNetCancelConnection2结束一个网络连接WNetCloseEnum结束一次枚举操作WNetConnectionDialog启动一个标准对话框,以便建立同网络资源的连接WNetDisconnectDialog启...
所谓API就是“应用程序接口”(ApplicationProgramingInterface),是一些用C语言编写,由操作系统自身调用的函数。WindowsAPI函数由许多“动态链接库”或DLL组成。在32位Windows中,核心的Windows APIDLL有如下一些: gdi32.dll----图形显示界面的API。 kernel32.dll----处理低级任务(比如内存和任务管理)的API。 user32.d11...
微信使用抢红包神器会不会被封号   近日,开始有媒体陆续报道,有网友在使用了抢红包神器自动抢到微信朋友圈里发出的红包后,发现自己的银行卡里的存款不翼而飞,或者信用卡无缘无故被盗刷。对此,微信团队表示,对于利用第三方软件刷抢红包的危害用户安全和体验的行为将给予严厉打击。 以下为微信团队声明全文: 春节越...