当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 不会被其他窗体遮挡的抓图,使用API的PrintWindow函数

ASP.NET
自定义控件(支持模板)
自定义控件(模板+数据绑定)
自定义控件(可以动态加载用户控件)
在代码隐藏中遍历当前页的所有控件
关于C#调用Office Web Components绘图的问题
利用Visual C#打造一个平滑的进度条
[VB] 防止程序运行多个实例
Visual Studio.Net 快捷键表
WinForm中ToolBar与TabControl的一些事件写法(C#)
【翻译】Managed DirectX(第六章)
利用.NET语言开发自己的脚本语言(一)
自动改变CheckBoxList选择项目的背景颜色
vb.net中windows服务的创建
BASE64编码规则及C#实现
.net中判断该应用程序是否已经启动,防止重复启动
XML Web Service 数据交换
开发花絮:一个DataList的ItemCommand事件意外
Snake.Net中的ORM(二)
ASP操作XML数据小结
可用来显示空值的时间选择控件4

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 58 ::
收藏到网摘: n/a


只要窗体的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 );