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


所属类别:.NET

文章作者:未知

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


只要窗体的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 NULLIntPtr 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 DCint nWidth, // width of bitmap, in pixelsint nHeight // height of bitmap, in pixels);[DllImport("gdi32.dll")]public static extern IntPtr SelectObject(IntPtr hdc, // handle to DCIntPtr 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);关闭本页

相关信息

· 简单实用 Web快速共享好轻松

· 使用自定义验证组件库扩展 Windows 窗体(二)

· Linux 的常用网络命令

· 移花接木 Windows XP两步变Vista








....

43332 61501