Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

screenshot bugs #20

Open
JoeQinOvO opened this issue Jul 14, 2023 · 1 comment
Open

screenshot bugs #20

JoeQinOvO opened this issue Jul 14, 2023 · 1 comment

Comments

@JoeQinOvO
Copy link

the screenshot doesn't seem to get my whole screen
image

@urbanknight
Copy link

urbanknight commented Jul 17, 2023

This happened if the user changed the display scaling. To solve this you need to calculate the actual screen size with that change.

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);

    public enum DeviceCap
    {
        VERTRES = 10,
        DESKTOPVERTRES = 117
    }

    public static double GetWindowsScreenScalingFactor(bool percentage = true)
    {
        //Create Graphics object from the current windows handle
        Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero);
        //Get Handle to the device context associated with this Graphics object
        IntPtr DeviceContextHandle = GraphicsObject.GetHdc();
        //Call GetDeviceCaps with the Handle to retrieve the Screen Height
        int LogicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.VERTRES);
        int PhysicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.DESKTOPVERTRES);
        //Divide the Screen Heights to get the scaling factor and round it to two decimals
        double ScreenScalingFactor = Math.Round(PhysicalScreenHeight / (double)LogicalScreenHeight, 2);
        //If requested as percentage - convert it
        if (percentage)
        {
            ScreenScalingFactor *= 100.0;
        }
        //Release the Handle and Dispose of the GraphicsObject object
        GraphicsObject.ReleaseHdc(DeviceContextHandle);
        GraphicsObject.Dispose();
        //Return the Scaling Factor
        return ScreenScalingFactor;
    }
    public static Size GetDisplayResolution()
    {
        var sf = GetWindowsScreenScalingFactor(false);
        var screenWidth = Screen.PrimaryScreen.Bounds.Width * sf;
        var screenHeight = Screen.PrimaryScreen.Bounds.Height * sf;
        return new Size((int)screenWidth, (int)screenHeight);
    } 

and you can use it like this:

var size = GetDisplayResolution();

and for better stealthy way use WinAPI to take the screenshot because "CopyFromScreen" will raise a flag by most AVs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants