All formats

ImageSharp

LanguageC# (.NET 8 or later)
LicenseSix Labors Split License: Apache 2.0 for open source projects and companies under $1M annual revenue, otherwise a commercial license
Repositorygithub.com/SixLabors/ImageSharp
Documentationdocs.sixlabors.com
Depends onnothing native (pure managed code)
Read / writeyes, all Windows and OS/2 header versions, 1-32 bit, RLE4, RLE8, RLE24, bit fields / yes, 1, 2, 4, 8, 16, 24 and 32-bit

ImageSharp is a cross-platform image library written entirely in C#. It runs on Windows, Linux and macOS without native dependencies, which makes it the usual replacement for System.Drawing in .NET 6 and later. It reads and writes BMP, PNG, JPEG, GIF, TIFF, WebP, TGA, ICO and more, and has a processing API for resize, crop, rotate and color operations.

Its BMP decoder handles the rare variants too: OS/2 headers, Adobe V3 headers, RLE24 and 1-bit images. The encoder writes any bit depth from 1 to 32; for 8 bits and below it quantizes the image to a palette. Check the license before using it in a commercial closed-source product.

Installation#

Shell
dotnet add package SixLabors.ImageSharp

Basic usage: read, resize and save a BMP#

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

using Image image = Image.Load("in.bmp");
Console.WriteLine($"{image.Width} x {image.Height}");

image.Mutate(x => x.Resize(image.Width / 2, 0));  // 0 keeps the aspect ratio
image.Save("out.bmp");  // encoder from the extension: 24-bit BMP by default

How to get BMP size and bit depth without decoding#

Image.Identify reads only the headers; the BMP metadata tells the bit depth and header version:

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Bmp;

ImageInfo info = Image.Identify("in.bmp");
BmpMetadata bmp = info.Metadata.GetBmpMetadata();
Console.WriteLine($"{info.Width} x {info.Height}, {(int)bmp.BitsPerPixel} bits, header {bmp.InfoHeaderType}");

How to save a BMP with a specific bit depth#

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Bmp;

using Image image = Image.Load("in.png");

// 32-bit with alpha (V4 header with an alpha mask)
image.SaveAsBmp("rgba.bmp", new BmpEncoder
{
    BitsPerPixel = BmpBitsPerPixel.Bit32,
    SupportTransparency = true,
});

// 8-bit palette, quantized to 256 colors
image.SaveAsBmp("palette.bmp", new BmpEncoder { BitsPerPixel = BmpBitsPerPixel.Bit8 });

// 1-bit black and white
image.SaveAsBmp("mono.bmp", new BmpEncoder { BitsPerPixel = BmpBitsPerPixel.Bit1 });

How to convert BMP to PNG#

C#
using SixLabors.ImageSharp;

using Image image = Image.Load("in.bmp");
image.SaveAsPng("out.png");

How to read a BMP from a stream or byte array#

C#
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

byte[] data = File.ReadAllBytes("in.bmp");
using Image<Rgb24> image = Image.Load<Rgb24>(data);
Rgb24 pixel = image[0, 0];  // top-left pixel
Console.WriteLine($"{pixel.R} {pixel.G} {pixel.B}");

Tips#