-1

I'm trying to print an Image from a file, but Graphics.DrawImage crops the image when I try to print it. Example: When I try to print the Mona Lisa the output is cropped

This seemes to be the same problem but the solution doesn't work for me: DrawImage scales original image

My Code:

private void print()
    {
        PrintDocument pd = new PrintDocument();
        
        pd.PrintPage += PrintPage;
        pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
        pd.Print();
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        Image img = Image.FromFile(@"C:\Users\Leres75\Desktop\MonaLisa.jpg");
        Rectangle rect = new Rectangle(0, 0, img.Width, img.Height);
        img.Save(@"C:\Users\Leres75\Desktop\TestOutput.jpg"); //not Cropped
        e.Graphics.DrawImage(img, rect);
    }

I tried different variants of the DrawImage method and tried messing aroung with my screen settings to change the DPI but the output doesn't change.

How do I print the whole image?

Community
  • 1
  • 1
Leres75
  • 1
  • 1
  • Is the image bigger than the page? The "cropped" version you have shown has a very similar H:V ratio to an A4 page, which makes me think you're simply trying to draw the image outside the bounds of the page. – ProgrammingLlama Dec 22 '18 at 15:36
  • _tried messing aroung with my screen settings to change the DPI but the output doesn't change._ Doubtful. If you set the _dpi_ (of the image!!) __correctly__ (and you __need__ to do this, no good keeping the random settings the images come with) the result will surely change.. – TaW Dec 22 '18 at 16:00
  • With the Mona Lisa the Image bounds are: Width = 687, Height= 1024 and the page bounds are Width = 827, Height = 1169 – Leres75 Dec 22 '18 at 16:26
  • this is the pdf that gets printed: [link](https://oszimt-my.sharepoint.com/:b:/g/personal/kessler_pavel_oszimt_onmicrosoft_com/EamoIF6mLhdCuBQEPczyGiYBXiCUvNG_HSL2mvnEOvtfYg?e=PfOqhA) – Leres75 Dec 22 '18 at 16:30
  • _image bounds are: Width = 687, Height= 1024_ - Do not __ignore__ the image's `dpi` ! `DrawImage` will not ignore it either!!! – TaW Dec 22 '18 at 19:00

1 Answers1

-1

Ok, I've got a way around it: I'm resizing the image so that it fits the page. This works for me since my images are in the A4 format anyway. I'm using this to resize the image: How to resize an Image C#

My code is now:

private void PrintPage(object o, PrintPageEventArgs e)
    {
        Image img = Image.FromFile(@"C:\Users\pavel\OneDrive - OSZ IMT\Desktop\MonaLisa.jpg");
        Image resizedImage = ResizeImage(img, e.PageSettings.PaperSize.Width, e.PageSettings.PaperSize.Height);
        e.Graphics.DrawImage(resizedImage, 0, 0);
    }
Leres75
  • 1
  • 1