PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jul 19, 2024 5:19 am

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: Fri Feb 17, 2012 12:02 pm 
Offline

Joined: Wed Jan 25, 2012 6:42 pm
Posts: 1
Hi,

Iam trying to save .pdf with multiple pages and compressed images as described here:
http://forum.pdfsharp.net/viewtopic.php?f=2&t=1825&p=5206&hilit=compress#p5206

In order to use compressed images in my document, I saved the images as compressed .jpeg to a stream.

Code:
EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)30);
ImageCodecInfo[] iciCodecs = ImageCodecInfo.GetImageEncoders();
EncoderParameters epParameters = new EncoderParameters(1);
epParameters.Param[0] = epQuality;
img.Save(stream, iciCodecs.Single((i) => i.MimeType == "image/jpeg"), epParameters);


Then I create the XImages from that stream...

Code:
var xImg = XImage.FromGdiPlusImage(System.Drawing.Image.FromStream(stream);


Unfortunately it does not work this way...Am I doing something wrong?

Here is my complete source:

Code:
public void AddImage(System.Drawing.Bitmap img, string path)
       {
           try
           {
               // Compress image first
               using (var stream = new MemoryStream())
               {
                   EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)30);
                   ImageCodecInfo[] iciCodecs = ImageCodecInfo.GetImageEncoders();
                   EncoderParameters epParameters = new EncoderParameters(1);
                   epParameters.Param[0] = epQuality;
                   img.Save(stream, iciCodecs.Single((i) => i.MimeType == "image/jpeg"), epParameters);

                   using (var xImg = XImage.FromGdiPlusImage(System.Drawing.Image.FromStream(stream)))
                   {
                       var page = _document.AddPage();
                       page.Width = XUnit.FromCentimeter(21).Point;
                       page.Height = XUnit.FromCentimeter(29.7).Point;

                       var rect = new XRect(
                           PixelToPoints(0, img.HorizontalResolution),
                           PixelToPoints(0, img.HorizontalResolution),
                           PixelToPoints(img.Width, img.HorizontalResolution),
                           PixelToPoints(img.Height, img.HorizontalResolution));

                       using (var gfx = XGraphics.FromPdfPage(page, XGraphicsUnit.Point))
                       {
                           gfx.DrawImage(XImage.FromGdiPlusImage(img), rect);
                       }
                   }
               }
           }
           catch (System.Exception ex)
           {
           }
       }


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 21, 2012 9:34 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
Hi!

Your code might work with the GDI+ build, but not with the WPF build.
Which build do you use?

Save to a temporary file to make it work with the WPF build (PDFsharp copies the original bytes into the PDF file - WPF won't give us the original bytes, so this works for files only, not streams; all other formats are converted to "PDF images", so these will work fine from streams).

_________________
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 27 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