Screen Capture target area of the desktop

為{幸葍}努か 提交于 2019-12-31 03:03:26

问题


I'm trying to recreate a Winform application in C# that has the same functionality as the snipping tool windows provides. That is, allowing the user to drag a rectangle over the desktop and capture what ever is inside as an image.

At the moment I only have the ability to draw a rectangle with the mouse, and that's within the winform. Can anyone point me in the direction of how to do it so I can do it within the whole desktop?

My code for drawing the rectangle is as follows:

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

I've been looking around online, but my searches haven't provided anything of use yet.

Any help would be greatly appreciated.


回答1:


See:

http://www.codeproject.com/Articles/485883/Create-your-own-Snipping-Tool

And:

http://www.codeproject.com/Articles/21913/TeboScreen-Basic-C-Screen-Capture-Application

as references of Snipping tool creation.

Hope this helps.




回答2:


See: http://everysolutionshere.blogspot.in/2014/01/how-to-capture-screen-in-c.html

I hope it is helpful to you.



来源:https://stackoverflow.com/questions/18316693/screen-capture-target-area-of-the-desktop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!