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

Pdf sharp and image resizing problem
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2688
Page 1 of 1

Author:  qbfinest [ Thu Dec 19, 2013 9:22 am ]
Post subject:  Pdf sharp and image resizing problem

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);
               
            }
        }
    }
}

Author:  Thomas Hoevel [ Thu Dec 19, 2013 2:59 pm ]
Post subject:  Re: Pdf sharp and image resizing problem

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.

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