Ali Tor
Ali Tor

Reputation: 3015

How to do a screenshot area selection by drawing on desktop to take screenshot?

I want to make an SS application. But I have problem on this subject. I want user to be able to select a special area to take screenshot. I also want the desktop is live while the user is selecting the area. For example user wants to take an SS of a video's specific frame. The user must be able to do this while video is playing. I have tried this using drawing directly on the desktop. But it flickers so much. How can I fix this or is there an alternative way to do?

My code :

    [DllImport("User32.dll")]
    static extern IntPtr GetDC(IntPtr hwnd);

    [DllImport("user32.dll")]
    static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);

    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
    }

    void Form1_Load(object sender, EventArgs e)
    {
        Start();
    }

    IntPtr handle;
    Graphics grp;

    void Start()
    {
        handle = GetDC(IntPtr.Zero);
        grp = Graphics.FromHdc(handle);
        grp.SmoothingMode = SmoothingMode.HighQuality;
        timer2.Start();
    }
    private void timer2_Tick(object sender, EventArgs e)
    {

        grp.DrawLine(Pens.Red, 0, Cursor.Position.Y, Screen.PrimaryScreen.Bounds.Width, Cursor.Position.Y);
        InvalidateRect(IntPtr.Zero, IntPtr.Zero, false);
    }

Upvotes: 6

Views: 580

Answers (1)

miroxlav
miroxlav

Reputation: 12204

Create form with semi-transparent (or fully transparent) background, which is always-on-top, borderless and in the size of the desktop. Do any screenshot rectangle selection graphics (e.g. selected rectangle + guides + magnifier, fully opaque) on that form. When selection is made by user, hide the form and take the screenshot.

Upvotes: 5

Related Questions