博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#如何抓屏

Posted on 2006-06-13 11:27  中晨  阅读(1181)  评论(0编辑  收藏  举报

      internal class NativeMethods
 
     {
 
          [DllImport("user32.dll")]
 
          public extern static IntPtr GetDesktopWindow();
 

 
          [System.Runtime.InteropServices.DllImport("user32.dll")]
 
          public static extern IntPtr GetWindowDC(IntPtr hwnd);
 

 
          [System.Runtime.InteropServices.DllImport("gdi32.dll")]
 
          public static extern UInt64 BitBlt
 
               (IntPtr hDestDC,
 
               int x,
 
               int y,
 
               int nWidth,
 
               int nHeight,
 
               IntPtr hSrcDC,
 
               int xSrc,
 
               int ySrc,
 
               System.Int32 dwRop);
 
     }
 

 
          // Save the screen capture into a jpg
 
          public void SaveScreen()
 
          {
 
               Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
 
                    Screen.PrimaryScreen.Bounds.Height);
 
               Graphics gr1 = Graphics.FromImage(myImage);
 
               IntPtr dc1 = gr1.GetHdc();
 
               IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow());
 
               NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width,
 
                    Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376);
 
               gr1.ReleaseHdc(dc1);
 
               myImage.Save("screenshot.jpg", ImageFormat.Jpeg);
 
          }

//主要是使用三个API函数来做的.也是从网上看来的并不是原创.