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

1

6

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);
    }
Vasily answered 19/12, 2015 at 16:3 Comment(1)
FYI - Greenshot does it, and it is free and open source .NET code. You can just "peek" at their code.Louise
R
5

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.

Repine answered 19/12, 2015 at 16:10 Comment(3)
If I didn't misunderstand, drawing on semi-transparent is transparent again as much as the form is. When I set the form opacity to 0.1 the selection rectangle doesn't seem alive. On the other hand I couldn't achieve the drawing on fully transparent form. Are you able to give a code example?Vasily
@AliTor – I know, I simplified that in my original explanation. Semi-transparent form would work as you say, but better approach is opaque form with transparent background – so only its controls (e.g. guidelines or selection frame) will remain opaque. You can take a look at some existing tools, which are successfully using this approach, e.g. SnagIt.Repine
Ok, I did it. I was wrong at the difference between setting opacity and setting transparency key for transparency. I have done it setting the backcolor of the form as transparency key value . Thanks @miroxlav.Vasily

© 2022 - 2024 — McMap. All rights reserved.