屏幕捕获桌面的目标区域

时间:2013-08-19 14:45:39

标签: c# winforms screenshot

我正在尝试在C#中重新创建一个Winform应用程序,该应用程序具有与剪切工具窗口提供的功能相同的功能。也就是说,允许用户在桌面上拖动一个矩形并捕获内部的图像。

目前我只能用鼠标绘制一个矩形,而且它在winform中。任何人都可以指出我如何做到这一点所以我可以在整个桌面上做到这一点吗?

我绘制矩形的代码如下:

 Rectangle rect; 
    public Form1()
    {
        InitializeComponent();

        // set the cursor to be a + sign
        this.Cursor = System.Windows.Forms.Cursors.Cross;
        this.DoubleBuffered = true;
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        // e.X and e.Y are used to get the X and Y pos of the mouse
        rect = new Rectangle(e.X, e.Y, 0, 0);
        this.Invalidate();
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            // draw rectangle as mouse moves
            rect = new Rectangle(rect.Left,rect.Top, e.X - rect.Left, e.Y - rect.Top);
        }
        this.Invalidate();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Replace "Color.Red" with any color and repalce "2" with any size you like.
        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
}

我一直在网上搜索,但我的搜索还没提供任何用途。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

相关问题