2010-07-13 47 views
6

Tôi đang sử dụng chức năng win32 PrintWindow để chụp màn hình với đối tượng BitMap.Cách chụp một phần của màn hình

Nếu tôi chỉ muốn chụp một vùng của cửa sổ, làm thế nào tôi có thể cắt hình ảnh trong bộ nhớ?

Đây là mã tôi đang sử dụng để nắm bắt toàn bộ cửa sổ:

[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; 
} 

Trả lời

3

Bạn chỉ có thể lấy toàn bộ màn hình và sau đó vượt qua các hình ảnh vào một chức năng cắt xén mà chọn một khu vực tổng hình ảnh. Hãy xem phương thức Bitmap.Clone(). ví dụ.

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; 
} 

Lưu ý, tôi kéo này xuống từ this blog

+0

Đó là những gì tôi đã suy nghĩ ... có thể thực sự lấy được phần đó nhưng điều này đơn giản hơn rất nhiều. – Adam

0

Lưu cho mình một số rắc rối và nhặt nguồn để Cropper.

3

Đây là mã hoàn chỉnh để chụp màn hình và tạo hình ảnh được cắt xén 100 pixel vuông. Mã được lấy từ một sự kiện bấm nút. Sử dụng những gì bạn cần.

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); 
       }  
      }     
     } 
Các vấn đề liên quan