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图片在你的广用程序文件夹中!

posted @ 2007-02-05 15:07  大力哥的技术  阅读(2269)  评论(3)    收藏  举报
版权
作者:Bober Song

出处:http://bober.cnblogs.com

Care健康:http://www.aicareyou.com

推荐空间:华夏名网

本文首发博客园,版权归作者跟博客园共有。

转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。