empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
533 stars 132 forks source link

IndexOutOfRangeException when displaying a PNG #79

Open julienrffr opened 10 months ago

julienrffr commented 10 months ago

Take the HelloWorld program of Core build, Add: var imageStream = File.Open(@"C:\Users\xxx\Downloads\MI6LOGO.png", FileMode.Open, FileAccess.Read); XImage xImage = XImage.FromStream(imageStream); gfx.DrawImage(xImage, 0, 0, 100, 100); Where MI6LOGO.png is this attached file: MI6LOGO It will throw an IndexOutOfRangeException at line https://github.com/empira/PDFsharp/blob/b28bc3265e0a15302c04b7b91da0d915a591e167/src/foundation/src/PDFsharp/src/PdfSharp/PngLib/RawPngData.cs#L54 because paletteIndex = 1 and data has 0 elements.

Stack trace: at PdfSharp.BigGustave.RawPngData.GetPixelIndex(Int32 x, Int32 y) in D:\gitroot\PDFsharp.net6\src\foundation\src\PDFsharp\src\PdfSharp\PngLib\RawPngData.cs:line 54 at PdfSharp.BigGustave.Png.GetPixelIndex(Int32 x, Int32 y) in D:\gitroot\PDFsharp.net6\src\foundation\src\PDFsharp\src\PdfSharp\PngLib\Png.cs:line 52 at PdfSharp.Drawing.Internal.ImageImporterPng.TestPngInfoHeader(StreamReaderHelper stream, ImportedImage ii) in D:\gitroot\PDFsharp.net6\src\foundation\src\PDFsharp\src\PdfSharp\Drawing.Internal\ImageImporterPng.cs:line 307 at PdfSharp.Drawing.Internal.ImageImporterPng.ImportImage(StreamReaderHelper stream) in D:\gitroot\PDFsharp.net6\src\foundation\src\PDFsharp\src\PdfSharp\Drawing.Internal\ImageImporterPng.cs:line 29

ThomasHoevel commented 9 months ago

I tried your code snippet with the PNG file downloaded from this thread - and everything worked as expected.

julienrffr commented 9 months ago

I tried your code snippet with the PNG file downloaded from this thread - and everything worked as expected.

@ThomasHoevel With Core build?

I hope also that github image storage does not alter the image by optimizing it. Or your browser.

Your reproduction scenario is visibly different from mine somewhere.

ThomasHoevel commented 9 months ago

I can reproduce it now. I thought I was using Core build, but VS used a different build. So far I don't see an easy fix.