68 ArgumentNullException.ThrowIfNull(stream);
69 ImageInfo info = SixLabors.ImageSharp.Image.Identify(stream);
70 if (info.Metadata.DecodedImageFormat.Name !=
"BMP") {
71 throw new FormatException($
"Image format({info.Metadata.DecodedImageFormat.Name}) is not Bmp");
74 result.Width = info.
Width;
75 result.Height = info.Height;
77 ?
throw new InvalidOperationException(
"Unsupported BMP pixel format.")
87 public static void Save(
Image image, Stream stream,
Format format,
bool sync =
false) {
88 ArgumentNullException.ThrowIfNull(image);
89 ArgumentNullException.ThrowIfNull(stream);
91 throw new InvalidOperationException(
"Unsupported BMP pixel format.");
93 BmpEncoder encoder =
new() { BitsPerPixel = bitsPerPixel };
103 ArgumentNullException.ThrowIfNull(stream);
104 if (!BitConverter.IsLittleEndian) {
105 throw new InvalidOperationException(
"Unsupported system endianness.");
107 byte[] array =
new byte[54];
108 if (stream.Read(array, 0, array.Length) != array.Length) {
109 throw new InvalidOperationException(
"Invalid BMP header.");
112 return result.Type1 != 66 || result.Type2 != 77 ?
throw new InvalidOperationException(
"Invalid BMP header.") :
113 result.Compression != 0 ?
throw new InvalidOperationException(
"Unsupported BMP compression.") : result;
117 {
Format.RGBA8, BmpBitsPerPixel.Pixel32 },
118 {
Format.RGB8, BmpBitsPerPixel.Pixel24 },
119 {
Format.Pixel1, BmpBitsPerPixel.Pixel1 },
120 {
Format.Pixel16, BmpBitsPerPixel.Pixel16 },
121 {
Format.Pixel2, BmpBitsPerPixel.Pixel2 },
122 {
Format.Pixel4, BmpBitsPerPixel.Pixel4 },
123 {
Format.Pixel8, BmpBitsPerPixel.Pixel8 }