PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Bitmap/Image to PDF - results in blurry image
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1811
Page 1 of 1

Author:  eightx2 [ Mon Oct 10, 2011 11:38 am ]
Post subject:  Bitmap/Image to PDF - results in blurry image

I'm using this version: "PDFSharp-MigraDocFoundation-1_31" (only the PDFsharp part).

I have a Bitmap in my C# (winforms) application. This bitmap is like a screenshot of the form.

When I save this bitmap to a file (.bmp or .png), it's totally fine. However, when I 'convert' this bitmap into a .pdf file using PDFsharp, the image becomes blurry! (That is, the image on the pdf page of course.)
I zoom in to 100% when viewing the PDF file in Adobe Reader. Also when I print the PDF file, it is blurry on the paper.
(The image/bitmap contains many text objects - therefore it's really easy to see that it's blurry.)

Here is my code:

Code:
Bitmap bitmap = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));

bitmap.Save(@"C:\test.png");
           
PdfDocument doc = new PdfDocument();
PdfPage page = new PdfPage();
           
XImage img = XImage.FromGdiPlusImage(bitmap);
img.Interpolate = false;

doc.Pages.Add(page);               

XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(img, 0, 0);
           
doc.Save(@"C:\test.pdf");
doc.Close();


I have already searched on the forum regarding this. Found viewtopic.php?p=2471#p2471 but didn't help.


Any help please?

Author:  Thomas Hoevel [ Mon Oct 10, 2011 11:57 am ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

eightx2 wrote:
Any help please?
I don't have that image, I don't have your PDF, I don't know which Adobe Reader you are using ...
What help do you expect?

Have you disabled the smoothing in Adobe Reader?
See Edit => Preferences, then under "Categories" select "Page Display" and clear all control boxes that have "smooth" in their label.

Author:  eightx2 [ Tue Oct 11, 2011 6:43 am ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

Thomas Hoevel wrote:
eightx2 wrote:
Any help please?
I don't have that image, I don't have your PDF, I don't know which Adobe Reader you are using ...
What help do you expect?

Have you disabled the smoothing in Adobe Reader?
See Edit => Preferences, then under "Categories" select "Page Display" and clear all control boxes that have "smooth" in their label.


Smoothing doesn't help - as I said, it's still blurry when I print it on paper.

I've made a new test (new code below) - it's a screenshot of my original post here and saved as png. I load this png and save it as pdf.

Code:
Bitmap bitmap = new Bitmap(@"C:\capture.png");

PdfDocument doc = new PdfDocument();
PdfPage page = new PdfPage();

XImage img = XImage.FromFile(@"C:\capture.png");
img.Interpolate = false;

doc.Pages.Add(page);

XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
xgr.DrawImage(img, 0, 0);

doc.Save(@"C:\capture.pdf");
doc.Close();


Attached is capture.png. Couldn't attach capture.pdf, so that one is here: http://www.sendspace.com/file/1lwb94

Attachments:
capture.png
capture.png [ 28.25 KiB | Viewed 40973 times ]

Author:  Thomas Hoevel [ Tue Oct 11, 2011 8:27 am ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

The image in the PDF file looks exactly like the PNG file:
Attachment:
not_blurry.png
not_blurry.png [ 81.72 KiB | Viewed 40969 times ]

The PNG is blurry because you have ClearType turned on. The PDF contains an unmodified copy of that PNG file (no blurriness added).

Author:  eightx2 [ Tue Oct 11, 2011 8:38 am ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

Thomas Hoevel wrote:
The image in the PDF file looks exactly like the PNG file:
Attachment:
not_blurry.png

The PNG is blurry because you have ClearType turned on. The PDF contains an unmodified copy of that PNG file (no blurriness added).

Not sure why you say that the PNG is blurry. It's not blurry here. Also, you must admit what when viewing the PDF in 100%, it looks really blurry?

So, let's say you're right. Then can you explain why the PDF still has that touch of blurriness when printed out on paper? (Nothing's wrong with the printer.)

Author:  Thomas Hoevel [ Tue Oct 11, 2011 11:49 am ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

eightx2 wrote:
Not sure why you say that the PNG is blurry.
I included a screenshot of MS Paint showing the PNG file at 400 %. It does not contain black and white only because ClearType was active when the screenshot (your PNG file) was taken and that's how ClearType works.
ClearType enhances resolution by making letters blurry ...

Here's another screenshot (ClearType was on on the left, off on the right, both at 500 %):
Attachment:
ClearType.png
ClearType.png [ 10.04 KiB | Viewed 40963 times ]

Anti-aliasing was used for both fonts, but ClearType adds colour to the image.

I included a screenshot showing no difference between your PNG file (viewed with MS Paint) and your PDF file (viewed with Adobe Reader).
No new blurriness in the PDF, all details like in the PNG.

Author:  rdavidson [ Wed Dec 21, 2011 4:37 pm ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

Thomas,

I am having the same issue with a bitmap image that is blurry when rendered on a pdf. Is there anything that can be done with the image before drawing it? I have tried InterpolationMode.HighQualityBiCubic and SmoothingMode.HighQuality but it does not really fix the issue.
The image is from a base64ZipString.
Thanks

Author:  () => true [ Thu Dec 22, 2011 6:53 pm ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

Hi!
rdavidson wrote:
I have tried InterpolationMode.HighQualityBiCubic and SmoothingMode.HighQuality but it does not really fix the issue.
PDFsharp does not resize or otherwise modify the image, so I presume these settings have absolutely no effect.

Have you tried this:
Code:
XImage image = XImage.FromFile(file);
image.Interpolate = false;
That's a hint to Adobe Reader how to display the image.
Let's see an image and the corresponding PDF so we can understand the problem.

Author:  rdavidson [ Fri Jan 13, 2012 3:46 pm ]
Post subject:  Re: Bitmap/Image to PDF - results in blurry image

What I did was to create a new bitmap - resizing to the scale that I needed and then setting the resolution back to what the original bitmap was.

Dim divideBy As Double
Dim divideByH As Double
Dim divideByW As Double

divideByW = bmImage.Width / 122 - 'desired width
divideByH = bmImage.Height / 54 'desired height

If divideByW < 1 Then
divideByW = 1.0
End If

If divideByH < 1 Then
divideByH = 1.0
End If
If divideByW > divideByH Then
divideBy = divideByW
Else
divideBy = divideByH
End If

finalImg = New Bitmap(CInt(CDbl(bmImage.Width) / divideBy), CInt(CDbl(bmImage.Height) / divideBy))
finalImg.SetResolution(bmImage.HorizontalResolution, bmImage.VerticalResolution)
gfx = Graphics.FromImage(finalImg)
gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gfx.DrawImage(bmImage, New Rectangle(0, 0, CInt(CDbl(bmImage.Width) / divideBy), CInt(CDbl(bmImage.Height) / divideBy)), 0, 0, bmImage.Width, bmImage.Height, GraphicsUnit.Pixel)
gfx.Dispose()

'clean up the image (take care of any image loss from resizing)
Dim lblImage As XImage = XImage.FromGdiPlusImage(finalImg)
gfxas.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
gfxas.Graphics.SmoothingMode = SmoothingMode.AntiAlias

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/