2010-07-13 51 views
6

我正在使用win32 PrintWindow函数来捕获屏幕到BitMap对象。如何捕获屏幕的一部分

如果我只想捕捉窗口的某个区域,如何在内存中裁剪图像?

这里是我使用捕捉整个窗口的代码:

[System.Runtime.InteropServices.DllImport(strUSER32DLL, CharSet = CharSet.Auto, SetLastError = true)] 
public static extern int PrintWindow(IntPtr hWnd, IntPtr hBltDC, uint iFlags); 

public enum enPrintWindowFlags : uint 
{ 
    /// <summary> 
    /// 
    /// </summary> 
    PW_ALL = 0x00000000, 
    /// <summary> 
    /// Only the client area of the window is copied. By default, the entire window is copied. 
    /// </summary> 
    PW_CLIENTONLY = 0x00000001 
} 

public System.Drawing.Bitmap CaptureWindow(IntPtr hWnd, enPrintWindowFlags eFlags) 
{ 
    System.Drawing.Rectangle rctForm = System.Drawing.Rectangle.Empty; 

    using(System.Drawing.Graphics grfx = System.Drawing.Graphics.FromHdc(GetWindowDC(hWnd))) 
    { 
     rctForm = System.Drawing.Rectangle.Round(grfx.VisibleClipBounds); 
    } 

    System.Drawing.Bitmap pImage = new System.Drawing.Bitmap(rctForm.Width, rctForm.Height); 
    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pImage); 

    IntPtr hDC = graphics.GetHdc();   
    //paint control onto graphics using provided options   
    try 
    {    
     PrintWindow(hWnd, hDC, (uint)eFlags);  
    } 
    finally 
    {    
     graphics.ReleaseHdc(hDC);   
    }  
    return pImage; 
} 

回答

3

你可以简单地抓取整个屏幕,然后将图像传送到该选择整个图像的区域的裁剪功能。看看Bitmap.Clone()方法。例如

public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight) 
{ 
Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight); 
Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat); 
return cropped; 
} 

注意,我拉下来,从this blog

+0

这就是我在想......可能实际上只是抓住那一部分,但这更简单。 – Adam 2010-07-13 20:42:42

3

下面是捕获屏幕并创建100像素平方的裁剪图像的完整代码。代码来自按钮Click事件。使用你需要的。

Bitmap screenShot = null; 
     Bitmap croppedImage; 
     Graphics screen; 

     if(saveFileDialog.ShowDialog() == DialogResult.OK) 
     { 
      this.Hide(); 
      screenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
            Screen.PrimaryScreen.Bounds.Height, 
            PixelFormat.Format32bppArgb); 
      screen = Graphics.FromImage(screenShot); 
      screen.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, 
            Screen.PrimaryScreen.Bounds.Y, 
            0, 
            0, 
            Screen.PrimaryScreen.Bounds.Size, 
            CopyPixelOperation.SourceCopy); 
      screenShot.Save(saveFileDialog.FileName, ImageFormat.Png); 
      this.Show(); 
     } 

     //crop image 
     if(screenShot != null) 
     { 
      if(saveFileDialog.ShowDialog() == DialogResult.OK) 
      { 
       int x = 100; 
       int y = 100; 
       int xWidth = 100; 
       int yHeight = 100; 
       Rectangle rect = new Rectangle(x, y, xWidth, yHeight); 
       croppedImage = screenShot.Clone(rect, PixelFormat.Format32bppArgb); 
       if (croppedImage != null) 
       { 
        croppedImage.Save(saveFileDialog.FileName, ImageFormat.Png); 
       }  
      }     
     }