PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 11:18 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Jun 12, 2020 3:39 pm 
Offline

Joined: Sat May 07, 2016 9:34 pm
Posts: 9
I recently updated my app to .NET Core 3.1 and reloaded the various packages in their core versions. One of those packages was PDFSharp, which I use to create PDF documents. They say that the basic package (v1.50.5147) is Core compatible.

In my documents I often write JPEG images. This worked in the .NET Framework version but doesn't seem to work in the Core version. All I see are empty spaces where the images should be.

I'm using the following:

Gfx.DrawImage(XImage.FromStream(ImageStream), (int)Left, (int)Bottom, (int)Width, (int)Height);

If I use XImage.FromFile on just a known file for all my images, I get 'somewhat better' results. i.e. some instances of the image show up in the PDF and some don't.

Is this familiar to anyone? Is there a workaround?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 15, 2020 8:11 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Hi!
rmcx wrote:
They say that the basic package (v1.50.5147) is Core compatible.
Who says that?

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 15, 2020 12:25 pm 
Offline

Joined: Sat May 07, 2016 9:34 pm
Posts: 9
I guess I misread the following line in their NuGet page:

Quote:
This is the PDFsharp PDF Core package


I saw the word
Quote:
Core
and jumped to the wrong conclusion.

Everything else I'm doing is working OK in .Net Core 3.1 and as I won't go back to Framework, I'll have to try to work around this issue.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 24, 2020 5:35 pm 
Offline

Joined: Sat May 07, 2016 9:34 pm
Posts: 9
In my app, I can use either XImage.FromFile() or XImage.FromStream() as needed.

If I use XImage.FromFile(), everything seems to be OK, including at least PNG, JPG, and TGA formats.

However, as a job might make use of the same image dozens or hundreds of times, I wanted to be able to cache the images once read off of the disk. To do this I load the images once and store the bitmaps in a dictionary cache. Then when I want to embed them in the PDF, I extract the saved image from the cache and do a Gfx.DrawImage.

Here's the code that did that properly using .NET Framework (using the Bitmap X case):

Code:
            XImage Image;
            switch (ImageObj)
            {
               case Bitmap X:
                  {
                     using MemoryStream ImageStream = new MemoryStream();
                     X.Save(ImageStream, ImageFormat.Bmp);
                     Image = XImage.FromStream(ImageStream);
                  }
                  break;

               case Stream X:
                  {
                     Image = XImage.FromStream(X);
                     X.Dispose();
                  }
                  break;

               case string X:
                  {
                     Image = XImage.FromFile(X);
                  }
                  break;

               default:
                  throw new FormatException();
            }

            /*  draw it  */

            Gfx.DrawImage(Image, (int)Left, (int)Bottom, (int)Width, (int)Height);


This fails all the time in .NET Core and .NET 5.0.

if I pull the MemoryStream declaration outside the switch case, then things work fine for PNG images in .NET 5.0. JPG and TGA images still don't work:
Code:
            XImage Image;
            using MemoryStream ImageStream = new MemoryStream();
            switch (ImageObj)
            {
               case Bitmap X:
                  {
                     X.Save(ImageStream, ImageFormat.Bmp);
                     Image = XImage.FromStream(ImageStream);
                  }
                  break;

               case Stream X:
                  {
                     Image = XImage.FromStream(X);
                     X.Dispose();
                  }
                  break;

               case string X:
                  {
                     Image = XImage.FromFile(X);
                  }
                  break;

               default:
                  throw new FormatException();
            }

            /*  draw it  */

            Gfx.DrawImage(Image, (int)Left, (int)Bottom, (int)Width, (int)Height);


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 147 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