C#项目里添加一个抓图功能

来源:百度知道 编辑:UC知道 时间:2024/06/28 20:12:16
如图,怎么样实现点击保存后将左上方pictruebox内容截图下来?来段代码。3Q

最简单的办法就是一楼那样 除此之外还有两种方法
第一种截屏的方法
//创建新图片
Bitmap img= new Bitmap(pictrueBox1.Width, pictruBox1.Height);
//获取画笔
Graphics gs = Graphics.FromImage(img);
//复制屏幕到画笔
gs.CopyFromScreen(this.left + pictrueBox1.left,this.top + pictruBox1.top, 0, 0, pictrueBox1.Width, pictrueBox1.Height);
//保存图片
img.save("0.bmp");
//释放画笔
gs.Disponse();

第二种api方法
//bitblt是句柄复制方法
[DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);

//获取pictrueBox画笔
Graphics g1 = this.pictureBox1.CreateGraphics();
//创建一个新图片
Bitmap img = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
//获取新图片的画笔
Graphics g2 = Graphics.FromImage(img);
//获取它们的句柄
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
//句柄复制
BitBlt(dc2, 0, 0, pictureBox1.Wi