标题: C#取得当前窗体图片
- 糊涂小猪 2007-04-11 15:55 阅读:779
- 评论:0 | 添加评论
Yesterday,客户对需求提出了一些变更,其中有提到一点就是要打印当前的确窗体.
想到的一个解决方案就是模拟按下ALT+PRNT的组合键,然后从CLIPBOARD中取的要打印的图形.
当然,第一步是要取得你当前的窗体..实现方法如下:
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hc,IntPtr hDest);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd,
ref RECT lpRect);
private Bitmap GetActiveForm()
{
IntPtr platDC = GetForegroundWindow();
IntPtr windDC = GetWindowDC(platDC);
Rect rect = new Rect();
GetWindowRect(windDC,ref rect);
Bitmap bmp = new Bitmap(rect.right - right.left,rect.bottom - rect.top);
Graphics g = Graphics.FromIamge(bmp);
IntPtr hdc = g.getHdc();
BitBlt( hdc,0,0,bmp.width,bmp.height,windDC,0,0,3278651);
g.ReleaseHdc(hdc);
g.dispose();
ReleaseDC(platDC,winDC);
return bmp;
}
private void print()
{
Bitmap bm = new Bitmap();
bm = GetActiveForm();
// past the bmp to clipboard
SendKeys.Send("%{PRTSC}");
Application.DoEvents();
// print bmp
IDataObject iData = Clipboard.GetDataObject ();
Image img = (Image)iData.GetData (DataFormats.Bitmap);
e.Graphics.DrawImage(img,0,0);
}
That's all!!