c# 抓取屏幕的小程序

 

/*
* 作者:Jerson Ju 
* 创建日期:2008年3月18号
* 描写:抓取屏幕的小程序
* 地点:长沙

*/

//只贴核心部分了,版面有限
[DllImport("User32.dll")]
  
public static extern IntPtr GetDC(
   IntPtr hWnd   
// handle to window
  );
[DllImport(
"Gdi32.dll")]
  
public static extern IntPtr SelectObject(
   IntPtr hdc,          
// handle to DC
   IntPtr hgdiobj   // handle to object
   );
[DllImport(
"Gdi32.dll")]
  
public static extern IntPtr CreateCompatibleDC(
   IntPtr hdc   
// handle to DC
   );
  [DllImport(
"Gdi32.dll")]
  
public static extern IntPtr CreateCompatibleBitmap(
   IntPtr hdc,        
// handle to DC
   int nWidth,     // width of bitmap, in pixels
   int nHeight     // height of bitmap, in pixels
   );
  [DllImport(
"Gdi32.dll")]
  
public static extern bool DeleteDC(
   IntPtr hdc   
// handle to DC
   );
  [DllImport(
"Gdi32.dll")]
  
public static extern bool BitBlt(
   IntPtr hdcDest, 
// handle to destination DC
   int nXDest,  // x-coord of destination upper-left corner
   int nYDest,  // y-coord of destination upper-left corner
   int nWidth,  // width of destination rectangle
   int nHeight, // height of destination rectangle
   IntPtr hdcSrc,  // handle to source DC
   int nXSrc,   // x-coordinate of source upper-left corner
   int nYSrc,   // y-coordinate of source upper-left corner
   uint dwRop  // raster operation code
   );
  
//获得全屏图片
  public Image getPic()
  
{
   Image img;
   IntPtr h 
= GetDC((IntPtr)0);
   IntPtr oldBit;
   IntPtr memDc 
= CreateCompatibleDC(h);
   IntPtr menBit 
= CreateCompatibleBitmap(h,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
   oldBit 
= SelectObject(memDc,menBit);
   BitBlt(memDc,
0,0,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,h,0,0,0x00CC0020);
   menBit 
= SelectObject(memDc,oldBit);
   DeleteDC(h);
   DeleteDC(memDc);
   img  
= Image.FromHbitmap(menBit);
   
return img;
  }
 


上面是调用WinAPI方式的,用.NET自带的类方式如下。

   Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
   Graphics gs 
= Graphics.FromImage(bmp);
      gs.CopyFromScreen(
0000new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
  
this.pictureBox1.Image = bmp;

 

 

posted @ 2008-04-26 22:09  debugzhu  阅读(490)  评论(1编辑  收藏  举报