界面如下:
下面放了一个PictureBox
首先是声明函数:
1 //这里是调用 Windows API函数来进行截图 2 //首先导入库文件 3 [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] 4 5 //声明函数 6 private static extern IntPtr CreateDC 7 ( 8 string Driver, //驱动名称 9 string Device, //设备名称 10 string Output, //无用,可以设定为null 11 IntPtr PrintData //任意的打印机数据 12 ); 13 14 15 [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] 16 private static extern bool BitBlt( 17 IntPtr hdcDest, //目标设备的句柄 18 int XDest, //目标对象的左上角X坐标 19 int YDest, //目标对象的左上角的Y坐标 20 int Width, //目标对象的宽度 21 int Height, //目标对象的高度 22 IntPtr hdcScr, //源设备的句柄 23 int XScr, //源设备的左上角X坐标 24 int YScr, //源设备的左上角Y坐标 25 Int32 drRop //光栅的操作值 26 27 );
截图按钮事件代码:
1 private void button1_Click(object sender, EventArgs e) 2 { 3 //创建显示器的DC 4 IntPtr dcScreen = CreateDC("DISPLAY", null, null, (IntPtr)null); 5 6 //由一个指定设备的句柄创建一个新的Graphics对象 7 Graphics g1 = Graphics.FromHdc(dcScreen); 8 int tmpWidth, tmpHeigth; 9 10 11 //获得保存图片的质量 12 long level = long.Parse(this.textBox5.Text); 13 14 //如果是全屏捕获 15 if(this.radioButton1.Checked == true) 16 { 17 tmpWidth = Screen.PrimaryScreen.Bounds.Width; 18 tmpHeigth = Screen.PrimaryScreen.Bounds.Height; 19 Image MyImage = new Bitmap(tmpWidth, tmpHeigth, g1); 20 21 //创建位图图形对象 22 Graphics g2 = Graphics.FromImage(MyImage); 23 //获得窗体的上下文设备 24 IntPtr dc1 = g1.GetHdc(); 25 //获得位图文件的上下文设备 26 IntPtr dc2 = g2.GetHdc(); 27 28 //写入到位图 29 BitBlt(dc2, 0, 0, tmpWidth, tmpHeigth, dc1, 0, 0, 13369376); 30 31 //释放窗体的上下文设备 32 g1.ReleaseHdc(dc1); 33 //释放位图的上下文设备 34 g2.ReleaseHdc(dc2); 35 36 37 //保存图像并显示 38 SaveImageWithQuality(MyImage,level); 39 this.pictureBox1.Image = MyImage; 40 41 } 42 else 43 { 44 int X = Convert.ToInt32(this.textBox1.Text); 45 int Y = Convert.ToInt32(this.textBox2.Text); 46 int Width = Convert.ToInt32(this.textBox3.Text); 47 int Height = Convert.ToInt32(this.textBox4.Text); 48 49 Image Myimage = new Bitmap(Width, Height,g1); 50 Graphics g2 = Graphics.FromImage(Myimage); 51 IntPtr dc1 = g1.GetHdc(); 52 IntPtr dc2 = g2.GetHdc(); 53 54 BitBlt(dc2,0,0,Width,Height,dc1,X,Y,13369376); 55 g1.ReleaseHdc(dc1); 56 g2.ReleaseHdc(dc2); 57 58 SaveImageWithQuality(Myimage, level); 59 this.pictureBox1.Image = Myimage; 60 61 62 } 63 }
设置图片质量的函数,详细介绍:http://www.cnblogs.com/zhaotianff/p/5570321.html
1 //保存图片时设置质量 2 private void SaveImageWithQuality(Image bmp,long level) 3 { 4 5 6 ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); 7 System.Drawing.Imaging.Encoder myEncoder = 8 System.Drawing.Imaging.Encoder.Quality; 9 EncoderParameters myEncoderParameters = new EncoderParameters(1); 10 EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, level); 11 myEncoderParameters.Param[0] = myEncoderParameter; 12 bmp.Save(@"test.jpg", jgpEncoder, myEncoderParameters); 13 14 15 16 } 17 18 19 20 21 22 private ImageCodecInfo GetEncoder(ImageFormat format) 23 { 24 25 ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); 26 27 foreach (ImageCodecInfo codec in codecs) 28 { 29 if (codec.FormatID == format.Guid) 30 { 31 return codec; 32 } 33 } 34 return null; 35 }
运行效果:
可惜把自己也截出来了,正在寻找方法不把自己截出来,哈哈。