PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 21, 2024 3:34 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Dec 19, 2013 9:22 am 
Offline

Joined: Thu Dec 19, 2013 9:12 am
Posts: 1
Hello,

I have a problem with image resizing. It seems like the image is cropped instead of resized. Unfortunately I cannot attach the files I am working on because of the forum restrictions.
The image I am testing with can be found at

http://d.lib.ncsu.edu/adore-djatoka/res ... 39.88-2004

Any pdf will do fine for testing.

Code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace PdfSharpTest
{
    class Program
    {
        private const int MarginForPictures = 15;

        static void Main(string[] args)
        {
            var pdfDocument = PdfReader.Open(EmptyPdf(), PdfDocumentOpenMode.Modify);
            AddImage(pdfDocument);
            pdfDocument.Save("D://result.pdf");
        }

        private static Stream EmptyPdf()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("PdfSharpTest.empty.pdf");
        }

        private static Stream TestImage()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("PdfSharpTest.resolver.jpeg");
        }

        private static void AddImage(PdfDocument pdfDocument)
        {
            using (var image = Image.FromStream(TestImage()))
            {
                var addPage = pdfDocument.AddPage();
                addPage.Size = PageSize.Letter;
                addPage.TrimMargins = new TrimMargins { All = MarginForPictures };
                var gfx = XGraphics.FromPdfPage(addPage);
                var pdfImage = XImage.FromGdiPlusImage(image);

                var margin = XUnit.FromMillimeter(MarginForPictures).Point;
                var widthFitRate = pdfImage.PointWidth / (addPage.Width.Point - (margin * 2));
                var heightFitRate = pdfImage.PointHeight / (addPage.Height.Point - (margin * 2));

                var fitRate = Math.Max(widthFitRate, heightFitRate);

                    var fitWidth = pdfImage.PointWidth / fitRate;
                    var fitHeight = pdfImage.PointHeight / fitRate;
                    gfx.DrawImage(pdfImage, margin, margin, fitWidth, fitHeight);
               
            }
        }
    }
}


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 19, 2013 2:59 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
It's a bug in Windows.
Run your application under Windows XP and it will work.

Sorry for the inconvenience, but Windows 7 and higher mess up grayscale JPEG files. Convert the image to RGB JPEG and it should also work.

Or apply this patch and use XImage.FromFile(), not XImage.FromGdiPlusImage() and it will also work.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 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