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

XImage.FromFile(.png) fails with 6.0.0-preview-2
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4439
Page 1 of 1

Author:  Francesca [ Sat May 06, 2023 1:08 am ]
Post subject:  XImage.FromFile(.png) fails with 6.0.0-preview-2

I get an "Unsupported image format" when trying to load and image from a file using PdfSharp.Drawing.XImage in a .NET 7.0 project using PDFSharp 6.0.0-preview-2 and trying to read a .png -- works for .jpg.

Code:
using (XImage image = XImage.FromFile(imageFilepath))

I am able to XFont, XBrushes and XGraphics to add text.

This is supposed to work, right? I read in the docs that .png is already supported.

Are there any tricks?

PS I do have
Code:
GlobalFontSettings.FontResolver = new FailsafeFontResolver();
in my Program.cs

PSS I also tried to load the same image from a stream with the same results.
Code:
MemoryStream ms = new MemoryStream();

using (FileStream fs = new FileStream(imageFilepath, FileMode.Open))
{
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
    {
        ms.Write(buffer, 0, bytesRead);
    }
}

XImage image = XImage.FromStream(ms);

Author:  Francesca [ Sat May 06, 2023 4:34 am ]
Post subject:  Re: XImage.FromFile(.png) fails with 6.0.0-preview-2

I figured it out... Seems that you're only suppoting 8-bit color depth as of now.

From ImageImporterPng.cs:
Code:
namespace PdfSharp.Drawing.Internal
{
    internal class ImageImporterPng : ImageImporterRoot, IImageImporter
    {
...
        private bool TestPngInfoHeader(StreamReaderHelper stream, ImportedImage ii)
        {
            stream.CurrentOffset = 16;
            uint dWord = stream.GetDWord(0, bigEndian: true);
            uint dWord2 = stream.GetDWord(4, bigEndian: true);
            byte @byte = stream.GetByte(8);
            byte byte2 = stream.GetByte(9);
...
            switch (byte2)
            {
                case 0:
                    {
                        DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(43, 2);
                        defaultInterpolatedStringHandler.AppendLiteral("Unsupported bit depth ");
                        defaultInterpolatedStringHandler.AppendFormatted(@byte);
                        defaultInterpolatedStringHandler.AppendLiteral(" for PNG color type ");
                        defaultInterpolatedStringHandler.AppendFormatted(byte2);
                        defaultInterpolatedStringHandler.AppendLiteral(".");
                        throw new Exception(defaultInterpolatedStringHandler.ToStringAndClear());
                    }
                case 2:
                    {
                        if (@byte == 8)
                        {
                            ii.Information.ImageFormat = ImageInformation.ImageFormats.RGB24;
                            break;
                        }

                        DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(43, 2);
                        defaultInterpolatedStringHandler.AppendLiteral("Unsupported bit depth ");
                        defaultInterpolatedStringHandler.AppendFormatted(@byte);
                        defaultInterpolatedStringHandler.AppendLiteral(" for PNG color type ");
                        defaultInterpolatedStringHandler.AppendFormatted(byte2);
                        defaultInterpolatedStringHandler.AppendLiteral(".");
                        throw new Exception(defaultInterpolatedStringHandler.ToStringAndClear());
                    }
                case 3:
                    {
                        if (@byte == 8)
                        {
                            ii.Information.ImageFormat = ImageInformation.ImageFormats.Palette8;
                            break;
                        }

                        DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(43, 2);
                        defaultInterpolatedStringHandler.AppendLiteral("Unsupported bit depth ");
                        defaultInterpolatedStringHandler.AppendFormatted(@byte);
                        defaultInterpolatedStringHandler.AppendLiteral(" for PNG color type ");
                        defaultInterpolatedStringHandler.AppendFormatted(byte2);
                        defaultInterpolatedStringHandler.AppendLiteral(".");
                        throw new Exception(defaultInterpolatedStringHandler.ToStringAndClear());
                    }
                case 6:
                    {
                        if (@byte == 8)
                        {
                            ii.Information.ImageFormat = ImageInformation.ImageFormats.ARGB32;
                            break;
                        }

                        DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(43, 2);
                        defaultInterpolatedStringHandler.AppendLiteral("Unsupported bit depth ");
                        defaultInterpolatedStringHandler.AppendFormatted(@byte);
                        defaultInterpolatedStringHandler.AppendLiteral(" for PNG color type ");
                        defaultInterpolatedStringHandler.AppendFormatted(byte2);
                        defaultInterpolatedStringHandler.AppendLiteral(".");
                        throw new Exception(defaultInterpolatedStringHandler.ToStringAndClear());
                    }
                default:
                    {
                        DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(28, 1);
                        defaultInterpolatedStringHandler.AppendLiteral("Unsupported PNG color type ");
                        defaultInterpolatedStringHandler.AppendFormatted(byte2);
                        defaultInterpolatedStringHandler.AppendLiteral(".");
                        throw new Exception(defaultInterpolatedStringHandler.ToStringAndClear());
                    }
            }

Author:  TH-Soft [ Mon May 08, 2023 8:33 am ]
Post subject:  Re: XImage.FromFile(.png) fails with 6.0.0-preview-2

The list of supported formats can be seen here:
https://docs.pdfsharp.net/PDFsharp/Over ... ge-formats

My boss said nobody needs PNG files with 16 colours.

Which format does the PNG file have which you could not read?

Author:  Francesca [ Thu May 11, 2023 3:59 pm ]
Post subject:  Re: XImage.FromFile(.png) fails with 6.0.0-preview-2

TH-Soft wrote:
My boss said nobody needs PNG files with 16 colours.

Which format does the PNG file have which you could not read?


Yes, the images that I am adding are with 16-bit colors. The images are created with ImageMagick libraries and they use 16-bit colors by default.

I rewrote my code to use different libraries and to create only 8-bit images and it works now.

My eye can see the difference so the quality is much less for photos but at least it works.

Thank you for your response.

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