C#截取全屏幕
首先导入:
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Collections;
using System.Drawing.Imaging;
using System.Threading;
因为程序中用到BitBlt()函数;
所以
[DllImport("gdi32.dll")]
private static extern int BitBlt(IntPtr hdcDest,int nXDest,int nYDest,
int nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,UInt32 dwRop);
最后就是在你想实现此功能的地方加入下面的函数
{
this.Hide();//如果你不想截取的图象中有此应用程序
Thread.Sleep(1000);
Rectangle rect = new Rectangle();
rect = Screen.GetWorkingArea(this);//获得当前屏幕的大小
Graphics g = this.CreateGraphics();
//创建一个以当前屏幕为模板的图象
Image myimage = new Bitmap(rect.Width, rect.Height, g);
//第二种得到全屏坐标的方法
// Image myimage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,g);
//创建以屏幕大小为标准的位图
Graphics gg = Graphics.FromImage(myimage);
IntPtr dc = g.GetHdc();//得到屏幕的DC
IntPtr dcdc = gg.GetHdc();//得到Bitmap的DC
BitBlt(dcdc, 0, 0, rect.Width, rect.Height, dc, 0, 0, 13369376);
//调用此API函数,实现屏幕捕获
g.ReleaseHdc(dc);//释放掉屏幕的DC
gg.ReleaseHdc(dcdc);//释放掉Bitmap的DC
myimage.Save(Application.StartupPath + @"\bob.jpg", ImageFormat.Jpeg);//以JPG文件格式来保存
this.Show();
}
得到的jpg图片在你的广用程序文件夹中!
出处:http://bober.cnblogs.com/
CARE健康网: http://www.aicareyou.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号