PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 9:11 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Mon Oct 10, 2011 11:38 am 
Offline

Joined: Mon Oct 10, 2011 11:12 am
Posts: 3
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 10, 2011 11:57 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 11, 2011 6:43 am 
Offline

Joined: Mon Oct 10, 2011 11:12 am
Posts: 3
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 40964 times ]
Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 11, 2011 8:27 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
The image in the PDF file looks exactly like the PNG file:
Attachment:
not_blurry.png
not_blurry.png [ 81.72 KiB | Viewed 40960 times ]

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 11, 2011 8:38 am 
Offline

Joined: Mon Oct 10, 2011 11:12 am
Posts: 3
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.)


Top
 Profile  
Reply with quote  
PostPosted: Tue Oct 11, 2011 11:49 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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 40954 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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 21, 2011 4:37 pm 
Offline

Joined: Wed Dec 21, 2011 4:03 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 22, 2011 6:53 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 339
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 13, 2012 3:46 pm 
Offline

Joined: Wed Dec 21, 2011 4:03 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 146 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group