PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
PDF sharp image scaling, losing quality https://forum.pdfsharp.net/viewtopic.php?f=2&t=2652 |
Page 1 of 1 |
Author: | dds [ Fri Nov 15, 2013 10:18 pm ] |
Post subject: | PDF sharp image scaling, losing quality |
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. |
Author: | dds [ Fri Nov 15, 2013 11:47 pm ] |
Post subject: | Re: PDF sharp image scaling, losing quality |
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. |
Author: | () => true [ Sat Nov 16, 2013 6:08 pm ] |
Post subject: | Re: PDF sharp image scaling, losing quality |
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. |
Author: | dds [ Sat Nov 16, 2013 8:42 pm ] |
Post subject: | Re: PDF sharp image scaling, losing quality |
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. |
Author: | Thomas Hoevel [ Mon Nov 18, 2013 10:41 am ] |
Post subject: | Re: PDF sharp image scaling, losing quality |
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. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |