C # print form - image is blurred

I would like to print my shape, but the printed image is very blurry. In fact, it is so blurry that I can not read it.

First, I take my form:

  [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

    internal void CaptureScreen()
    {
        Application.DoEvents();

        Graphics mygraphics = frm.CreateGraphics();
        Size s = frm.Size;
        memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        IntPtr dc1 = mygraphics.GetHdc();
        IntPtr dc2 = memoryGraphics.GetHdc();
        BitBlt(dc2, 0, 0, frm.ClientRectangle.Width, frm.ClientRectangle.Height, dc1, 0, 0, 13369376);
        mygraphics.ReleaseHdc(dc1);
        memoryGraphics.ReleaseHdc(dc2);
    }

The screen image here is clean and clean.

Here is the OnPrintPage method:

private void OnPrintPage(object sender, PrintPageEventArgs e)
    {
        Bitmap img;
        int width = e.MarginBounds.Width;
        int height = e.MarginBounds.Height;

        if (memoryImage.Size.Width > width || memoryImage.Size.Height > height)
        {
            img = ResizeImage(memoryImage, new Size(width, height));
        }
        else
        {
            img = memoryImage;
        }


        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
        e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

        e.Graphics.DrawImage(img, e.MarginBounds.Top, e.MarginBounds.Left);
    }

Resize Method:

private static Bitmap ResizeImage(Bitmap imgToResize, Size size)
    {
        int sourceWidth = imgToResize.Width;
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)size.Width / (float)sourceWidth);
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBilinear;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
        g.Dispose();

        return b;
    }

I know that resizing an image makes it blurry. But drawing on a printed page erases it even more.

Are there any tricks for sharpening the image? Am I doing something wrong?

Thank you all.

+3
source share
2 answers

, , InterpolationMode. HighQualityBicubic InterpolationMode.HighQualityBilinear.

, Bitmap - DrawToBitmap.

+2

, : , ? ? ? , /, . , . , "" . .

: 96 120 /. - 600 1200 /. 10 x = 100 .

+1
source

Source: https://habr.com/ru/post/1718996/


All Articles