diff --git a/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs b/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs index 277bccb31..93645845f 100644 --- a/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs +++ b/src/UglyToad.PdfPig/Images/ColorSpaceDetailsByteConverter.cs @@ -87,7 +87,10 @@ private static Span UnpackComponents(Span input, int bitsPerComponen private static Span RemoveStridePadding(Span input, int strideWidth, int imageWidth, int imageHeight, int multiplier) { - Span result = new byte[imageWidth * imageHeight * multiplier]; + int size = imageWidth * imageHeight * multiplier; + Span result = size < input.Length ? input.Slice(0, size) : new byte[size]; + // See PDFBOX-492-4.jar-8.pdf, page 2 for size > input.Length + for (int y = 0; y < imageHeight; y++) { int sourceIndex = y * strideWidth;