如何获取截屏的预定义区域坐标值?

时间:2011-05-21 10:18:54

标签: c# .net winforms bitmap screenshot

  

可能重复:
  .NET Equivalent of Snipping Tool

下面的代码是截取整个屏幕的截图,但我想截取预定义区域的屏幕截图。我更喜欢单击按钮然后拖动并选择我想要抓取的区域x,y,destinationX,destinationY值。有人可以给我一个提示或示例如何做到吗?

bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
                           Screen.PrimaryScreen.Bounds.Height, 
                           PixelFormat.Format32bppArgb);

// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);

// Take the screenshot from the upper left corner to the right bottom corner                    
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                             Screen.PrimaryScreen.Bounds.Y,
                             0,
                             0,
                             Screen.PrimaryScreen.Bounds.Size,
                             CopyPixelOperation.SourceCopy);

1 个答案:

答案 0 :(得分:0)

我没有真正做过多位图/图形工作,但是你可以直接捕获mouse_down和mouse_up事件的X,Y坐标,然后在CopyFromScreen方法中使用它们

类似的东西:

 private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        startX = e.X;
        startY = e.Y;
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        endX = e.X;
        endY = e.Y;
    }

然后,您可以进行一些数学计算,以确定要传输的区域的大小,并将其输入到您的方法中。