截取桌面图像,比较简单,只需要简单的调用Graphics的CopyFromScreen方法即可以实现;
关于将图像绘制到窗口上很容易实现,但是却很少看到有文章介绍从窗口上截取图像的。下面主要介绍一下关于窗口图像截取的方法。
要截取窗口的图像,需要用到系统提供的BitBlt函数,这个函数的作用就是从源设备的上下文中拷贝一张Bitmap图像至目标设备。具体参数介绍请参见MSDN文档
下面是C#对该函数的引入操作:
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
具体操作代码参见如下:
Graphics gSrc = this.CreateGraphics(); //创建窗体的Graphics对象
HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc());
int width = this.Width-SystemInformation.FrameBorderSize.Width; //获取宽度
int height = this.Height-SystemInformation.FrameBorderSize.Height; //获取高度
const int SRCCOPY = 0xcc0020; //复制图块的光栅操作码
Bitmap bmSave = new Bitmap(width, height); //用于保存图片的位图对象
Graphics gSave = Graphics.FromImage(bmSave); //创建该位图的Graphics对象
HandleRef hDcSave = new HandleRef(null, gSave.GetHdc()); //得到句柄
BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY);
如此操作,当前窗体的图像即被保存之bmSave中。
下面提供一段详细的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace ScreenShot { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnScreen_Click(object sender, EventArgs e) { int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width; //屏幕宽度 int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height; //屏幕高度 Bitmap bmSave = new Bitmap(screenWidth, screenHeight); Graphics g = Graphics.FromImage(bmSave); g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy); g.Dispose(); SaveFile(bmSave); } private void btnWindow_Click(object sender, EventArgs e) { Graphics gSrc = this.CreateGraphics(); //创建窗体的Graphics对象 HandleRef hDcSrc = new HandleRef(null, gSrc.GetHdc()); int width = this.Width-SystemInformation.FrameBorderSize.Width; //获取宽度 int height = this.Height-SystemInformation.FrameBorderSize.Height; //获取高度 const int SRCCOPY = 0xcc0020; //复制图块的光栅操作码 Bitmap bmSave = new Bitmap(width, height); //用于保存图片的位图对象 Graphics gSave = Graphics.FromImage(bmSave); //创建该位图的Graphics对象 HandleRef hDcSave = new HandleRef(null, gSave.GetHdc()); //得到句柄 BitBlt(hDcSave, 0, 0, width, height, hDcSrc, 0, 0, SRCCOPY); gSrc.ReleaseHdc(); gSave.ReleaseHdc(); gSrc.Dispose(); gSave.Dispose(); SaveFile(bmSave); } private void SaveFile(Bitmap bmSave) { if (DialogResult.OK == saveFileDialog1.ShowDialog()) { string fileName = saveFileDialog1.FileName; if (1 == saveFileDialog1.FilterIndex) { if (!fileName.EndsWith(".bmp")) { fileName += ".bmp"; } } else if (2 == saveFileDialog1.FilterIndex) { if (!fileName.EndsWith(".jpg")) { fileName += ".jpg"; } } else if (3 == saveFileDialog1.FilterIndex) { if (!fileName.EndsWith(".png")) { fileName += ".png"; } } Save(bmSave, fileName); } } private void Save(Bitmap bm,string fileName) { if (fileName.EndsWith(".bmp")) { bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); } else if (fileName.EndsWith(".jpg")) { bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); } else if (fileName.EndsWith(".png")) { bm.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); } bm.Dispose(); } [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop); } }
窗体设计部分代码就省略了,测试窗体图像如下: