diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 01bdbd1c0..7819b1ebd 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -262,7 +262,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp private void Write24Bit(Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { - using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 3)) + int width = pixels.Width; + int rowBytesWithoutPadding = width * 3; + using (IManagedByteBuffer row = this.AllocateRow(width, 3)) { for (int y = pixels.Height - 1; y >= 0; y--) { @@ -270,8 +272,8 @@ namespace SixLabors.ImageSharp.Formats.Bmp PixelOperations.Instance.ToBgr24Bytes( this.configuration, pixelSpan, - row.GetSpan(), - pixelSpan.Length); + row.Slice(0, rowBytesWithoutPadding), + width); stream.Write(row.Array, 0, row.Length()); } } @@ -286,7 +288,9 @@ namespace SixLabors.ImageSharp.Formats.Bmp private void Write16Bit(Stream stream, Buffer2D pixels) where TPixel : unmanaged, IPixel { - using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 2)) + int width = pixels.Width; + int rowBytesWithoutPadding = width * 2; + using (IManagedByteBuffer row = this.AllocateRow(width, 2)) { for (int y = pixels.Height - 1; y >= 0; y--) { @@ -295,7 +299,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp PixelOperations.Instance.ToBgra5551Bytes( this.configuration, pixelSpan, - row.GetSpan(), + row.Slice(0, rowBytesWithoutPadding), pixelSpan.Length); stream.Write(row.Array, 0, row.Length()); @@ -341,7 +345,8 @@ namespace SixLabors.ImageSharp.Formats.Bmp using IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds()); ReadOnlySpan quantizedColors = quantized.Palette.Span; - PixelOperations.Instance.ToBgra32(this.configuration, quantizedColors, MemoryMarshal.Cast(colorPalette)); + var quantizedColorBytes = quantizedColors.Length * 4; + PixelOperations.Instance.ToBgra32(this.configuration, quantizedColors, MemoryMarshal.Cast(colorPalette.Slice(0, quantizedColorBytes))); Span colorPaletteAsUInt = MemoryMarshal.Cast(colorPalette); for (int i = 0; i < colorPaletteAsUInt.Length; i++) {