How to convert Base64 to BMP
- Paste the Base64 or the full
data:image/bmp;base64,...URI, or open a.txtfile with the string. Multi-megabyte strings are normal for bitmaps and are fine here. - The status line should say Detected BMP. The preview shows the image and its pixel size; Size gives the decoded file size in bytes.
- Press Download file to save the
.bmp.
Browsers decode BMP in an <img>, including 1-bit monochrome, 8-bit palette, 24-bit and most 32-bit files.
BMP file size from width and height
A BMP has a 14-byte file header, an info header (40 bytes in the common version), an optional palette and then the pixel rows. Each row is padded to a multiple of 4 bytes. For a 24-bit image without a palette:
file size = 54 + height × (width × 3, rounded up to a multiple of 4)
| Image | Row size | BMP file | Base64 length |
|---|---|---|---|
| 33×10, 24-bit | 99 → 100 bytes | 1,054 bytes | 1,408 chars |
| 100×100, 24-bit | 300 bytes | 30,054 bytes | 40,072 chars |
| 800×600, 24-bit | 2,400 bytes | 1,440,054 bytes | 1,920,072 chars |
| 1920×1080, 24-bit | 5,760 bytes | 6,220,854 bytes | 8,294,472 chars |
| 1920×1080, 1-bit | 240 bytes | 259,262 bytes | 345,684 chars |
The 1-bit row adds an 8-byte, two-color palette. Because the numbers are fixed, a bitmap is easy to check: the file header stores the total size in bytes 2 to 5. In the hex dump of the 100×100 example they read 66 75 00 00, which is 0x7566 = 30,054 as a little-endian number. If Size shows less than that, the Base64 was truncated. BMP rows are stored bottom-up by default, so a cut-off bitmap loses the top of the picture.
What BMP Base64 looks like
Every BMP starts with the letters BM, so the Base64 starts with Qk. The third character depends on the file size and is 0, 1, 2 or 3; Qk02 and Qk1m are typical. The detector also checks the info header size at byte 14, so random data that happens to start with BM is not mistaken for a bitmap.
Headerless bitmaps (DIB)
Some sources hand you the bitmap without the BM file header. This is a DIB, the format of the Windows clipboard (CF_DIB), of images inside ICO files and of some device SDKs. Its Base64 starts with KAAAA, the encoding of the 40-byte info header size 28 00 00 00.
The tool cannot preview it, since without the file header the bytes are not a valid image file, and it reports binary data. The fix is to put the 14 missing bytes in front:
import base64, struct
def dib_to_bmp(dib: bytes) -> bytes:
header_size, = struct.unpack_from("<I", dib, 0)
bpp, = struct.unpack_from("<H", dib, 14)
compression, colors = struct.unpack_from("<I", dib, 16)[0], struct.unpack_from("<I", dib, 32)[0]
if bpp <= 8 and colors == 0:
colors = 1 << bpp
masks = 12 if header_size == 40 and compression == 3 else 0
offset = 14 + header_size + masks + colors * 4
return b"BM" + struct.pack("<IHHI", 14 + len(dib), 0, 0, offset) + dib
b64 = open("dib.txt").read()
with open("image.bmp", "wb") as f:
f.write(dib_to_bmp(base64.b64decode(b64)))
The header needs the total file size and the offset of the pixel data, which follows the info header, the color masks of 16- and 32-bit bitfield images, and the palette. Encode the result again with BMP to Base64 if you need it as text, or check it here. DIBs from ICO files are a special case: their height is doubled to make room for a transparency mask. Base64 in Python has more on decoding binary data.
Common problems
- “This looks like PNG, not BMP”. The sender already converted the image, or the system only calls it a bitmap. Save it as PNG with Download as .png; it is smaller and every program reads it.
- The colors look swapped or the image is upside down in another program. BMP stores pixels as blue-green-red and usually bottom-up. A negative height in the header means top-down rows. Browsers handle all of this; simple custom readers sometimes do not.
- A device rejects the decoded file. Compare its SHA-256 with what the device received, then check the bit depth and header version in the hex dump. Older readers expect a 40-byte header (
28 00 00 00at offset 14) and no alpha channel.
FAQ
Why is my bitmap Base64 string so long?
BMP stores raw pixels without compression. A 1920×1080 24-bit bitmap is 6,220,854 bytes, which is 8,294,472 characters of Base64, no matter what the picture shows.
My Base64 starts with KAAAA instead of Qk. What is it?
A bitmap without its 14-byte file header, called a DIB. It comes from the Windows clipboard, some device SDKs and ICO files. Add the header with the Python snippet on this page and it becomes a normal .bmp.
How can I tell if a BMP string is complete?
Bytes 2 to 5 of the file hold its total size. Open Show hex dump, read them as a little-endian number and compare it with the Size row. If the decoded size is smaller, the string was cut off.
Can I get a PNG instead of a BMP?
This page saves the bitmap as it is. To convert, download it and drop it into BMP to Base64 with Convert to PNG; PNG is lossless and usually far smaller.