PDFsharp & MigraDoc Foundation

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

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: Sat May 06, 2023 1:08 am 
Offline

Joined: Fri May 05, 2023 10:55 pm
Posts: 3
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);


Top
 Profile  
Reply with quote  
PostPosted: Sat May 06, 2023 4:34 am 
Offline

Joined: Fri May 05, 2023 10:55 pm
Posts: 3
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());
                    }
            }


Top
 Profile  
Reply with quote  
PostPosted: Mon May 08, 2023 8:33 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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?

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


Top
 Profile  
Reply with quote  
PostPosted: Thu May 11, 2023 3:59 pm 
Offline

Joined: Fri May 05, 2023 10:55 pm
Posts: 3
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.


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