/// <summary> /// Encodes the image to the specified stream from the <see cref="ImageFrame{TPixel}"/>. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> public void Encode <TPixel>(Image <TPixel> image, Stream stream) where TPixel : struct, IPixel <TPixel> { Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); // Cast to int will get the bytes per pixel short bpp = (short)(8 * (int)this.bitsPerPixel); int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); this.padding = bytesPerLine - (image.Width * (int)this.bitsPerPixel); var infoHeader = new BmpInfoHeader( headerSize: BmpInfoHeader.Size, height: image.Height, width: image.Width, bitsPerPixel: bpp, planes: 1, imageSize: image.Height * bytesPerLine, clrUsed: 0, clrImportant: 0); var fileHeader = new BmpFileHeader( type: 19778, // BM offset: 54, reserved: 0, fileSize: 54 + infoHeader.ImageSize); #if NETCOREAPP2_1 Span <byte> buffer = stackalloc byte[40]; #else byte[] buffer = new byte[40]; #endif fileHeader.WriteTo(buffer); stream.Write(buffer, 0, BmpFileHeader.Size); infoHeader.WriteTo(buffer); stream.Write(buffer, 0, 40); this.WriteImage(stream, image.Frames.RootFrame); stream.Flush(); }
/// <summary> /// Encodes the image to the specified stream from the <see cref="ImageFrame{TPixel}"/>. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> public void Encode <TPixel>(Image <TPixel> image, Stream stream) where TPixel : struct, IPixel <TPixel> { Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); // Cast to int will get the bytes per pixel short bpp = (short)(8 * (int)this.bitsPerPixel); int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); this.padding = bytesPerLine - (image.Width * (int)this.bitsPerPixel); // Set Resolution. ImageMetaData meta = image.MetaData; int hResolution = 0; int vResolution = 0; if (meta.ResolutionUnits != PixelResolutionUnit.AspectRatio) { if (meta.HorizontalResolution > 0 && meta.VerticalResolution > 0) { switch (meta.ResolutionUnits) { case PixelResolutionUnit.PixelsPerInch: hResolution = (int)Math.Round(UnitConverter.InchToMeter(meta.HorizontalResolution)); vResolution = (int)Math.Round(UnitConverter.InchToMeter(meta.VerticalResolution)); break; case PixelResolutionUnit.PixelsPerCentimeter: hResolution = (int)Math.Round(UnitConverter.CmToMeter(meta.HorizontalResolution)); vResolution = (int)Math.Round(UnitConverter.CmToMeter(meta.VerticalResolution)); break; case PixelResolutionUnit.PixelsPerMeter: hResolution = (int)Math.Round(meta.HorizontalResolution); vResolution = (int)Math.Round(meta.VerticalResolution); break; } } } var infoHeader = new BmpInfoHeader( headerSize: BmpInfoHeader.Size, height: image.Height, width: image.Width, bitsPerPixel: bpp, planes: 1, imageSize: image.Height * bytesPerLine, clrUsed: 0, clrImportant: 0, xPelsPerMeter: hResolution, yPelsPerMeter: vResolution); var fileHeader = new BmpFileHeader( type: 19778, // BM offset: 54, reserved: 0, fileSize: 54 + infoHeader.ImageSize); #if NETCOREAPP2_1 Span <byte> buffer = stackalloc byte[40]; #else byte[] buffer = new byte[40]; #endif fileHeader.WriteTo(buffer); stream.Write(buffer, 0, BmpFileHeader.Size); infoHeader.WriteTo(buffer); stream.Write(buffer, 0, 40); this.WriteImage(stream, image.Frames.RootFrame); stream.Flush(); }