0
votes

I got two different screenshot result on different screen resolution, First I try with 1920*1080(which was my pc default resolution) got zoomed not desired result and later I tried with changing my pc resolution 1280*720 and this time works fine. Here is my sample code:

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Width);
        using (Graphics G = Graphics.FromImage(bmp))
        {
            G.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
            G.CopyFromScreen(this.PointToScreen(new Point(0, 0)), new Point(0, 0), ClientSize);
            double percent = 0.60;
            Color darken = Color.FromArgb((int)(255 * percent), Color.Black);
            using (Brush brsh = new SolidBrush(darken))
            {
                G.FillRectangle(brsh, this.ClientRectangle);
            }
        }
        saveDialog.FileName = "test";
        saveDialog.DefaultExt = "jpg";
        saveDialog.Filter = "JPG images (*.jpg)|*.jpg";

        if (saveDialog.ShowDialog() == DialogResult.OK)
        {
            var fileName = saveDialog.FileName;
            if (!System.IO.Path.HasExtension(fileName) || System.IO.Path.GetExtension(fileName) != "jpg")
                fileName = fileName + ".jpg";

            bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }

Image captured on 1920*1080 resolution: zoomed image

Image captured on 1280*720 resolution: Fine image

How screen resolution affects capturing screenshot? How can I get desired result on my pc default resolution(1920*180)? Please help. Thanks.

1

1 Answers

1
votes

FYI, I got the code which is work very nicely to take a screenshot. But I didn't check thoroughly how many features are there. So I just share my code with as answer, I hope it will be useful for you. Some dlls required you can add easily.

CODE:

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

namespace myScreenCapture
{
    public enum CaptureMode
    {
        Screen, Window
    }

    public static class ScreenCapturer
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

        [StructLayout(LayoutKind.Sequential)]
        private struct Rect
        {
            public int Left, Top, Right, Bottom;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetDesktopWindow();

        public static void CaptureAndSave(string filename, string path, CaptureMode mode = CaptureMode.Window, ImageFormat format = null)
        {
            ImageSave(filename, format, Capture(mode), path);
        }

        public static void CaptureAndSave(string filename, IntPtr handle, string path, ImageFormat format = null)
        {
            ImageSave(filename, format, Capture(handle), path);
        }

        public static void CaptureAndSave(string filename, Control c, string path, ImageFormat format = null)
        {
            ImageSave(filename, format, Capture(c), path);
        }

        public static Bitmap Capture(CaptureMode mode = CaptureMode.Window)
        {
            return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
        }


        public static Bitmap Capture(Control c)
        {
            return Capture(c.Handle);
        }

        public static Bitmap Capture(IntPtr handle)
        {
            Rectangle bounds;
            var rect = new Rect();
            GetWindowRect(handle, ref rect);
            bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
            CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);

            var result = new Bitmap(bounds.Width, bounds.Height);
            using (var g = Graphics.FromImage(result))
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);

            return result;
        }

        /// <summary> Position of the cursor relative to the start of the capture </summary>
        public static Point CursorPosition;

        static void ImageSave(string filename, ImageFormat format, Image image, string path)
        {
            format = format ?? ImageFormat.Png;
            if (!filename.Contains("."))
                filename = filename.Trim() + "." + format.ToString().ToLower();

            if (!filename.Contains(@"\"))
            {
                // filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? @"C:\Temp", filename);
                filename = Path.Combine(path, filename);
            }

            filename = filename.Replace("%NOW%", DateTime.Now.ToString("[email protected]"));
            image.Save(filename, format);
        }
    }
}

Use like:

ScreenCapturer.CaptureAndSave("ScreenShot_" + DateTime.Now.ToString("dd-MM-yyyy@TT@HH_mm_ss"), path, CaptureMode.Screen, System.Drawing.Imaging.ImageFormat.Jpeg);