Search code examples
c#winformsscreenshot

Screen shoot quality worse than normal


I am trying to capture a part of my screen. The problem is even if I use png to save the image the quality still worse than if I would just use normal print screen.

Here is the code I use:

        //display a save file dialog for the user to set the file name
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.Filter = "PNG (*.png)|*.png";
        saveFileDialog.FilterIndex = 0;
        saveFileDialog.RestoreDirectory = true;

        //if the user proceed saving the picture
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            //simplify code with constant numbers for demo

            //get the width of the panel we need the screenshoot off
            int x = 10;
            //get the height of the panel we need the screenshoot off
            int y = 10;
            //create the ractangle of the screenshoot panel
            Rectangle rect = new Rectangle(x, y, 5, 5);
            //create new bitmap
            Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bmp);

            //get the screenshoot of the panel
            g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);

            string fileName = saveFileDialog.FileName;
            if (!fileName.Contains(".png"))
                fileName += ".png";

            bmp.Save(fileName, ImageFormat.Png);
        }

EDIT:

Example image form what I take with code: enter image description here

Normal screenshot:

enter image description here

It does not look so different here, but it is worst.


Solution

  • The top image in your question was rescaled, smaller than the original. This is noticeable in images that contain fine detail, like the ClearType anti-aliasing pixels used to make the text more readable. When they get rescaled, the visual effect is ruined and text looks a lot worse.

    It is entirely unclear why the image was rescaled, nothing in your code could cause that. Double-check by using the debugger to inspect the bmp.HorizontalResolution property, it should match the DPI of your video adapter. Simplest explanation that it was done by whatever image viewing program you used, perhaps to make the image fit the window. Try zooming out.