PDFsharp icon indicating copy to clipboard operation
PDFsharp copied to clipboard

IndexOutOfRangeException when displaying a PNG

Open julienrffr opened this issue 1 year ago • 3 comments

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

julienrffr avatar Jan 23 '24 14:01 julienrffr

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

ThomasHoevel avatar Feb 07 '24 15:02 ThomasHoevel

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.

julienrffr avatar Feb 07 '24 17:02 julienrffr

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.

ThomasHoevel avatar Feb 08 '24 09:02 ThomasHoevel