Benutzer mit den meisten Antworten
Printing of images(WPF)

Frage
-
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); }
- Bearbeitet Hartl_D Montag, 25. November 2019 08:33
Antworten
-
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.
- Als Antwort markiert Hartl_D Montag, 2. Dezember 2019 08:11
Alle Antworten
-
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)
-
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. -
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.
- Als Antwort markiert Hartl_D Montag, 2. Dezember 2019 08:11