H
H
HPositron2020-10-21 20:45:59
Screen resolution
HPositron, 2020-10-21 20:45:59

How to get full screen shot in C#?

My program takes a screenshot and saves it to disk.
Here is the snapshot code:

Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save("D:\\1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
My monitor has a resolution of 1920x1080, but the top-left fragment of 1536x864 is saved as a screenshot.
How to take a screenshot in the correct resolution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-10-21
@HPositron

Something tells me that you have a multiplication factor of 1.25

[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public enum DeviceCap
{
    VERTRES = 10,
    DESKTOPVERTRES = 117,

    // http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
}  


private float getScalingFactor()
{
    Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    IntPtr desktop = g.GetHdc();
    int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
    int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); 

    float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight;

    return ScreenScalingFactor; // 1.25 = 125%
}

#region License

// // Разработано: Коротенко Владимиром Николаевичем (Vladimir N. Korotenko)
// // email: [email protected]
// // skype:vladimir-korotenko
// // https://vkorotenko.ru
// // Создано:   09:11:2020 14:45

#endregion

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Kvn.ScreenShooter
{
    public class ScreenShooter
    {
        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        private enum DeviceCap
        {
            // ReSharper disable once InconsistentNaming
            VERTRES = 10,

            // ReSharper disable once InconsistentNaming
            DESKTOPVERTRES = 117,
            // http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
        }


        private static float GetScalingFactor()
        {
            using (var g = Graphics.FromHwnd(IntPtr.Zero))
            {
                var desktop = g.GetHdc();
                var logicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
                var physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
                var scalingFactor = (float)physicalScreenHeight / (float)logicalScreenHeight;
                return scalingFactor; // 1.25 = 125%
            }

        }

        /// <summary>
        /// Получение скриншота с фактором увеличения у экрана
        /// </summary>
        /// <param name="path">Путь для сохранения файла</param>
        /// <param name="format">Формат сохраняемого файла</param>
        /// <param name="quality">Quality of jpeg</param>
        public static void TakeScreenShoot(string path, ImageFormat format, long quality = 75L)
        {
            var sc = GetScalingFactor();
            var width = (int)(SystemInformation.VirtualScreen.Width * sc);
            var height = (int)(SystemInformation.VirtualScreen.Height * sc);

            using (var shoot = new Bitmap(width, height))
            using (var graphics = Graphics.FromImage(shoot))
            {
                graphics.CopyFromScreen(0, 0, 0, 0, shoot.Size);
                if (path.ToLowerInvariant().EndsWith(".jpg") || path.ToLowerInvariant().EndsWith(".jpeg"))
                {
                    var jpgEncoder = GetEncoder(ImageFormat.Jpeg);
                    var myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    var encParameters = new EncoderParameters(1);
                    var myEncoderParameter = new EncoderParameter(myEncoder, quality);
                    encParameters.Param[0] = myEncoderParameter;
                    shoot.Save(path, jpgEncoder, encParameters);
                    return;
                }
                shoot.Save(path, format);
            }
        }
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            return ImageCodecInfo.GetImageDecoders().FirstOrDefault(codec => codec.FormatID == format.Guid);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question