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

抓取屏幕:
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以上)。

 

posted on 2010-09-07 14:14  和轩僮  阅读(740)  评论(0编辑  收藏  举报

导航