none
Printing of images(WPF) RRS feed

  • Question

  • Hallo,

    I want to print an image from type Windows.Control or Windows.Drawing. If i print the image directly via the following code, the image will be blurred. If i convert it to an PDF by ‘Microsoft PDF Converter’ and then print, the image will be clearer. What can I do that the image will be clearer if I print directly (sending to printer without converting)?


     
     PrintDocument pd = new PrintDocument();
     pd.PrintPage += Pd_PrintPage;
     pd.Print();
    
     private void Pd_PrintPage(object sender, PrintPageEventArgs e)
        {
    
          var pd = sender as PrintDocument;
          Rectangle m = new Rectangle(10,10, (int)pd.PrinterSettings.DefaultPageSettings.PrintableArea.Height-10, (int)pd.PrinterSettings.DefaultPageSettings.PrintableArea.Width-10);
          e.Graphics.DrawImage(image, m);
        }

    • Edited by Hartl_D Monday, November 25, 2019 8:33 AM
    Sunday, November 24, 2019 8:09 PM

Answers

  • Hart,

    You should maintain the original image pixels width to height aspect ratio. Otherwise you will distort the image by fitting it to the printer rectangle shape which is most likely not the same as the image rectangle.

    not working code (note I use 2 * border = 20):

       aspectratio = img.height / img.width

       w = pd.PrinterSettings.DefaultPageSettings.PrintableArea.Width - 20)

       h = w * aspectratio

       Rectangle m = new Rectangle(10,10, w, h )

    you may need to switch w and h depending on portrait or landscape mode.

    When you make the pdf you are defining this better with the pdf printer and I suspect it has a border around the image and etc. When you print yourself you need to do this.

    Furthermore, depending what you are doing exactly, the pdf may be printing closer to the image original size in pixels where you are enlarging it more with your code.

    • Marked as answer by Hartl_D Monday, December 2, 2019 8:11 AM
    Tuesday, November 26, 2019 12:12 PM

All replies

  • I'm not sure if you are using WPF, but if you are...

    There is a setting for controls called UseLayoutRounding, it makes things look much-better/less-fuzzy.

    FrameworkElement.UseLayoutRounding Property


    George Frias (Wikidot AWWshop)

    Sunday, November 24, 2019 8:29 PM
  • Hi Hartl_D, 

    Thank you for posting here.

    Generally speaking, printing the image directly is the same as printing the image by using PDF, so the problem may related to your printer.

    Are there any additional problems when converting to PDF and print?

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 26, 2019 9:28 AM
    Moderator
  • Hart,

    You should maintain the original image pixels width to height aspect ratio. Otherwise you will distort the image by fitting it to the printer rectangle shape which is most likely not the same as the image rectangle.

    not working code (note I use 2 * border = 20):

       aspectratio = img.height / img.width

       w = pd.PrinterSettings.DefaultPageSettings.PrintableArea.Width - 20)

       h = w * aspectratio

       Rectangle m = new Rectangle(10,10, w, h )

    you may need to switch w and h depending on portrait or landscape mode.

    When you make the pdf you are defining this better with the pdf printer and I suspect it has a border around the image and etc. When you print yourself you need to do this.

    Furthermore, depending what you are doing exactly, the pdf may be printing closer to the image original size in pixels where you are enlarging it more with your code.

    • Marked as answer by Hartl_D Monday, December 2, 2019 8:11 AM
    Tuesday, November 26, 2019 12:12 PM