简体   繁体   中英

Print scaled image c#

I'm trying to print image with this code.

   private void Print()
     {
         PrintDocument printDocument = new PrintDocument();
         printDocument.PrintPage += PrintDocument_PrintPage;

         PrintPreviewDialog printDialog = new PrintPreviewDialog();
         printDialog.Document = printDocument;

         DialogResult result = printDialog.ShowDialog();
         if (result == DialogResult.OK) printDocument.Print();

         printDocument.PrintPage -= PrintDocument_PrintPage;
     }

     private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
     {
      //e.Graphics.DrawImage(img, e.PageBounds.X, e.PageBounds.Y);
        e.Graphics.DrawImage(img, e.PageBounds.X, e.PageBounds.Y, 
             e.PageBounds.Width, e.PageBounds.Height);
     }

When image is small it prints fine, but when its hi-rez (I have image 992*1403) it draws with wrong size. When I view PreviewDialog or draw it to the pdf file it prints fine, but when I print it on printer it prints wrong sized.

Fixed my problem with this code:

e.Graphics.DrawImage(img, 0, 0, e.PageSettings.PrintableArea.Width, e.PageSettings.PrintableArea.Height);

Just a guess, but how will the application know the PageBounds without knowing what type of paper you are printing on?

Have you tried setting your paper size to a specific size using something like this:

printDocument .DefaultPageSettings.PaperSize = 
                new PaperSize("Custom", someWidth, someHeight);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM