PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Image Failure in .Net Core Version?
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4145
Page 1 of 1

Author:  rmcx [ Fri Jun 12, 2020 3:39 pm ]
Post subject:  Image Failure in .Net Core Version?

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?

Author:  TH-Soft [ Mon Jun 15, 2020 8:11 am ]
Post subject:  Re: Image Failure in .Net Core Version?

Hi!
rmcx wrote:
They say that the basic package (v1.50.5147) is Core compatible.
Who says that?

Author:  rmcx [ Mon Jun 15, 2020 12:25 pm ]
Post subject:  Re: Image Failure in .Net Core Version?

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.

Author:  rmcx [ Wed Jun 24, 2020 5:35 pm ]
Post subject:  Re: Image Failure in .Net Core Version?

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

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