PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Oct 19, 2024 8:37 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Nov 15, 2013 10:18 pm 
Offline

Joined: Fri Nov 15, 2013 7:27 pm
Posts: 4
Hi, thanks for this great tool. However, I'm having a problem when resizing/scaling an image to fit the page. This is the image I'm trying to put in the pdf:
http://imgur.com/XNUEXJp
And when I put it in the pdf (with the width set to match the width of the pdf and the height proportionally set) this is what I get
Dropbox link to pdf: https://www.dropbox.com/s/eh2wk9stc71vyzc/67405_IO000001_Caries_Normal_SCALED.pdf
Image of pdf: http://imgur.com/NbJ2SSU

As you can see I get significat loss of resolution, and the gray scale isn't nearly as clear.
In addition to showing an image of the pdf I have also attached it. For some reason the generated pdf (395 KB) is even smaller than the image drawn onto it (1.98 MB)!
Here is the code I used to do the image scaling and drawing:
Code:
        static void Main(string[] args)
        {
            // Create a new PDF document
            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Draw image
            const string imagePath = "imagepath";
            XImage xImage = XImage.FromFile(imagePath);
            double height = page.Width / xImage.PixelWidth * xImage.PixelHeight;
            gfx.DrawImage(xImage, 0, 0, page.Width, height);

            // Save the document...
            const string filename = "filename";
            document.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }


When I don't scale the image it is to large to fit the page. How can I scale the image without losing resolution? For example, here is what the image looks like when I convert it to pdf with a tool on my computer (and what I'm trying to do with pdf sharp):
Dropbox link to pdf: https://www.dropbox.com/s/36xfpjizx9vyecd/67405_IO000001_Caries_Normal.pdf
Image of pdf: http://imgur.com/NJY4Ule

Thank you for your time.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 15, 2013 11:47 pm 
Offline

Joined: Fri Nov 15, 2013 7:27 pm
Posts: 4
I have fixed this problem by using C#'s System.Drawing and System.Drawing.Imaging to rescale the image before using pdfsharp to draw it.


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 16, 2013 6:08 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 347
Hi!
dds wrote:
As you can see I get significat loss of resolution, and the gray scale isn't nearly as clear.
PDFsharp does not alter images.
But PDFsharp relies on the operating system to get the image data. And sometimes Windows reduces the number of grey scales in that process. So if you try your original code with different image formats (PNG, TIFF, BMP, GIF, ...) some should work without losses.
JPEG images are stored as they are, all other images are packed.

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


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 16, 2013 8:42 pm 
Offline

Joined: Fri Nov 15, 2013 7:27 pm
Posts: 4
Thanks for the quick reply. Is there any way I can prevent the OS (windows in my case) from reducing gray scales? In other words, how could I get lossless image displaying. Also I'm using PNG if it matters.


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 18, 2013 10:41 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3111
Location: Cologne, Germany
dds wrote:
I have fixed this problem by using C#'s System.Drawing and System.Drawing.Imaging to rescale the image before using pdfsharp to draw it.
You don't have to rescale it. Maybe it's enough to load the image and pass an image (not a filename) to PDFsharp to prevent the OS from messing the image up.

PDFsharp uses .NET and most people use it under Windows - but some Windows versions are buggier than others.
There are two builds of PDFsharp (GDI+ and WPF) and I don't recall if both builds suffer from this OS problem.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 63 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group