抓取屏幕或窗体并保存成图片

本文介绍了使用C#实现屏幕抓取的方法,包括整体屏幕抓取及特定窗口或控件的截图过程。提供了详细的代码示例,展示了如何利用Graphics类进行屏幕复制,并将图像保存为文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

抓取屏幕:
rectangle r = system.windows.forms.screen.primaryscreen.bounds;  
image img = new bitmap(r.width, r.height);  
graphics g = graphics.fromimage(img);  
g.copyfromscreen(new point(0, 0), new point(0, 0), new size(r.width, r.height));。  
intptr dc = g.gethdc(); 
g.releasehdc(dc);  
g.dispose();  
img .save("c:a.jpg");  
或  
private static extern bool bitblt(intptr hdcdest,int nxdest,int nydest,int nwidth,int nheight,intptr hdcsrc,int nxsrc,int nysrc,int32 dwrop);  

private image catchscreen()
{
bitmap bmpcatched = new bitmap(this.tablelayoutpanel1.width + 1, this.tablelayoutpanel1.height + 1);
graphics g = graphics.fromimage(bmpcatched);
system.drawing.rectangle rect = new system.drawing.rectangle(10, 10, 500, 400);
  g.copyfromscreen(new point(this.left + this.tablelayoutpanel1.location.x + 7, this.tablelayoutpanel1.location.y + (panelregisted.height - tablelayoutpanel1.height) / 2 - 1), new point(0, 0), this.panelregisted.clientrectangle.size);
  g.drawrectangle(new pen(color.black), 0, 0, bmpcatched.width - 1, bmpcatched.height - 1);
  image image = bmpcatched; 
  return image;
  }

保存窗体或控件:
     Rectangle r = Screen.PrimaryScreen.Bounds;
            Bitmap bmp = new Bitmap(r.Width,r.Height);
            this.DrawToBitmap(bmp, r);
            bmp.Save("d:/aa.jpg");
    注:窗体及控件皆有DRAWTOBITMAP这个方法(FW2.0以上)。

 

转载于:https://www.cnblogs.com/vic_lu/archive/2010/09/07/1820581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值