Browse Source

Using Seek requires consideration of whether this stream allows Seek and the case where the stream start position is not 0.

pull/2579/head
舰队的偶像-岛风酱! 3 years ago
parent
commit
e252a6f223
No known key found for this signature in database GPG Key ID: 71F5B3A2B181950C
  1. 13
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

13
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -133,6 +133,9 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
Guard.NotNull(image, nameof(image));
Guard.NotNull(stream, nameof(stream));
// Stream may not at 0.
long basePosition = stream.Position;
Configuration configuration = image.Configuration;
ImageMetadata metadata = image.Metadata;
BmpMetadata bmpMetadata = metadata.GetBmpMetadata();
@ -187,7 +190,7 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
this.WriteBitmapInfoHeader(stream, infoHeader, buffer, infoHeaderSize);
this.WriteImage(configuration, stream, image);
WriteColorProfile(stream, iccProfileData, buffer);
WriteColorProfile(stream, iccProfileData, buffer, basePosition);
stream.Flush();
}
@ -271,16 +274,20 @@ internal sealed class BmpEncoderCore : IImageEncoderInternals
/// <param name="stream">The stream to write to.</param>
/// <param name="iccProfileData">The color profile data.</param>
/// <param name="buffer">The buffer.</param>
private static void WriteColorProfile(Stream stream, byte[]? iccProfileData, Span<byte> buffer)
/// <param name="basePosition">The Stream may not be start with 0.</param>
private static void WriteColorProfile(Stream stream, byte[]? iccProfileData, Span<byte> buffer, long basePosition)
{
if (iccProfileData != null)
{
// The offset, in bytes, from the beginning of the BITMAPV5HEADER structure to the start of the profile data.
int streamPositionAfterImageData = (int)stream.Position - BmpFileHeader.Size;
stream.Write(iccProfileData);
long position = stream.Position; // Storage Position
BinaryPrimitives.WriteInt32LittleEndian(buffer, streamPositionAfterImageData);
stream.Position = BmpFileHeader.Size + 112;
_ = stream.Seek(basePosition, SeekOrigin.Begin);
_ = stream.Seek(BmpFileHeader.Size + 112, SeekOrigin.Current);
stream.Write(buffer[..4]);
_ = stream.Seek(position, SeekOrigin.Begin); // Reset Position
}
}

Loading…
Cancel
Save