【问题标题】:How to capture part of a screen如何截取屏幕的一部分
【发布时间】:2011-03-15 13:10:30
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: c# winapi screen-scraping screen-capture


    【解决方案1】:

    您可以简单地抓取整个屏幕,然后将图像传递给一个裁剪函数,该函数会选择整个图像的一个区域。看一下 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 拉下来的

    【讨论】:

    • 这就是我的想法……实际上可能只抓住那部分,但这要简单得多。
    【解决方案2】:

    省点麻烦,把源代码发到Cropper

    【讨论】:

      【解决方案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);
                     }     
                  }                   
               }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 2015-03-14
        • 2013-06-20
        • 1970-01-01
        相关资源
        最近更新 更多