All formats

BMP (Windows Bitmap)

Full nameWindows Bitmap, Device-Independent Bitmap (DIB)
Extensions.bmp, .dib
MIME typeimage/bmp (RFC 7903); also seen: image/x-bmp, image/x-ms-bmp
Typeraster image, usually uncompressed (optional RLE)
Starts with42 4D (BM)
SpecificationBitmap Storage (Microsoft Learn)
Maintained byMicrosoft
Licensedocumented by Microsoft, free to implement, no patents

BMP stores a raster image as a small header followed by the pixel values, usually without compression. It supports 1, 4 and 8-bit palette images and 16, 24 and 32-bit color. Because the pixels are stored almost as they are in memory, BMP files are easy to read and write without a library, which makes the format common in Windows programs, embedded systems, bootloaders, scanners and teaching code.

The same structure without the 14-byte file header, called a DIB (device-independent bitmap), is used on the Windows clipboard (CF_DIB), inside ICO and CUR icon files and in Windows resources. A .dib file is usually a regular BMP file with a different extension, but some programs (Pillow, for one) write .dib files without the file header.

When to use BMP#

File structure#

A BMP file has up to 5 parts, in this order:

Part Size Description
File header (BITMAPFILEHEADER) 14 bytes signature BM, file size, offset of the pixel data
DIB header (BITMAPINFOHEADER or later) 12-124 bytes width, height, bits per pixel, compression, resolution, palette size
Bit masks 12 or 16 bytes only with BI_BITFIELDS and a 40-byte header
Color table (palette) 4 bytes per color B, G, R, 0 entries; required for 1, 4 and 8 bits per pixel
Pixel array rest of the file rows of pixels, each padded to a multiple of 4 bytes

All integers are little-endian. A V5 header can also point to an embedded ICC color profile, stored after the pixel array.

File header#

Offset Size Field Description
0 2 bfType 42 4D (BM)
2 4 bfSize size of the file in bytes (don’t rely on it, some writers get it wrong)
6 4 bfReserved1, bfReserved2 0
10 4 bfOffBits offset from the start of the file to the pixel array

DIB header versions#

The first 4 bytes of the DIB header give its size, which identifies the version:

Size Name Adds
12 BITMAPCOREHEADER (OS/2 1.x) 16-bit width and height, palette entries of 3 bytes
40 BITMAPINFOHEADER the common header, read by every program
64 OS22XBITMAPHEADER (OS/2 2.x) OS/2 extensions (Huffman 1D, RLE24 compression)
108 BITMAPV4HEADER red, green, blue and alpha masks, color space, gamma
124 BITMAPV5HEADER rendering intent, embedded or linked ICC profile

Fields of the 40-byte BITMAPINFOHEADER, at offset 14 in the file:

Offset Size Field Description
14 4 biSize header size: 40
18 4 biWidth width in pixels (signed)
22 4 biHeight height in pixels; positive = rows stored bottom-up, negative = top-down
26 2 biPlanes always 1
28 2 biBitCount bits per pixel: 1, 4, 8, 16, 24 or 32
30 4 biCompression compression type, see below
34 4 biSizeImage size of the pixel data; may be 0 for BI_RGB
38 4 biXPelsPerMeter horizontal resolution (2835 = 72 DPI, 3780 = 96 DPI)
42 4 biYPelsPerMeter vertical resolution
46 4 biClrUsed number of palette entries; 0 means 2bits
50 4 biClrImportant usually 0, ignored

Pixel data#

Compression#

Value Name Description
0 BI_RGB uncompressed, by far the most common
1 BI_RLE8 run-length encoding for 8-bit images
2 BI_RLE4 run-length encoding for 4-bit images
3 BI_BITFIELDS uncompressed 16 or 32-bit with explicit color masks
4 BI_JPEG pixel array is a JPEG file (printers only)
5 BI_PNG pixel array is a PNG file (printers only)

RLE stores pairs of (count, color index) plus escape codes for end of line, end of bitmap and position jumps. It only helps for images with long runs of one color. BI_JPEG and BI_PNG are meant for printer drivers and almost no image program reads them.

Example: a 2 x 2 24-bit BMP#

42 4D                     signature "BM"
46 00 00 00               file size: 70 bytes
00 00 00 00               reserved
36 00 00 00               pixel data at offset 54
28 00 00 00               DIB header size: 40 (BITMAPINFOHEADER)
02 00 00 00               width: 2
02 00 00 00               height: 2 (positive: bottom-up)
01 00                     planes: 1
18 00                     bits per pixel: 24
00 00 00 00               compression: BI_RGB
10 00 00 00               image size: 16 bytes
13 0B 00 00 13 0B 00 00   resolution: 2835 x 2835 pixels per meter (72 DPI)
00 00 00 00 00 00 00 00   colors used, colors important
00 00 FF  FF FF FF  00 00 bottom row: red, white, 2 bytes padding
FF 00 00  00 FF 00  00 00 top row: blue, green, 2 bytes padding

Converting BMP files#

Specifications and documentation#

Software that opens BMP files#

SoftwareWindowsmacOSLinuxWebCanNotes
SumatraPDF✓viewfree, open source; opens .bmp and .dib
Microsoft Photos✓view, editbuilt into Windows
Paint✓view, edit, create, convertbuilt into Windows, saves monochrome, 16, 256 color and 24-bit BMP
macOS Preview✓view, edit, convertbuilt into macOS
Web browsers✓✓✓✓viewChrome, Edge, Firefox, Safari. Also on Android and iOS
GIMP✓✓✓view, edit, create, convertfree, open source; choose RLE, 16, 24 or 32-bit on export
Paint.NET✓view, edit, create, convertfree
Krita✓✓✓view, edit, createfree, open source
IrfanView✓view, convertfree for non-commercial use, batch conversion
XnView MP✓✓✓view, convertfree for private use
Gwenview✓viewKDE image viewer, open source
Adobe Photoshop✓✓view, edit, create, convertcommercial
ImageMagick✓✓✓convert, createcommand line, open source
Photopea✓view, edit, create, convertfree, runs in the browser

Libraries#

Open-source libraries that read and write BMP. See all BMP libraries for a comparison.

C/C++SDL, stb_image
C#ImageSharp, Magick.NET
JavaImageIO, TwelveMonkeys
Goimaging, x/image/bmp
PythonOpenCV, Pillow
RubyMiniMagick
Rustimage
PHPGD, Imagick, Intervention Image