Yalantis/uCrop · error · CImgIOException
load_bmp(): Invalid file_size %d specified in filename '%s'
Error message
load_bmp(): Invalid file_size %d specified in filename '%s' (expected %lu).
What it means
The BMP header's file_size field (bytes 0x02-0x05) does not match the actual size of the file on disk (fsiz), so load_bmp() throws CImgIOException. Many BMP writers put 0 or a stale value in this field even though the pixel data may be fine.
Source
Thrown at ucrop/src/main/jni/CImg.h:56802
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid BMP file '%s'.",
cimg_instance,
filename?filename:"(FILE*)");
}
// Read header and pixel buffer.
int
file_size = header[0x02] + (header[0x03]<<8) + (header[0x04]<<16) + (header[0x05]<<24),
offset = header[0x0A] + (header[0x0B]<<8) + (header[0x0C]<<16) + (header[0x0D]<<24),
header_size = header[0x0E] + (header[0x0F]<<8) + (header[0x10]<<16) + (header[0x11]<<24),
dx = header[0x12] + (header[0x13]<<8) + (header[0x14]<<16) + (header[0x15]<<24),
dy = header[0x16] + (header[0x17]<<8) + (header[0x18]<<16) + (header[0x19]<<24),
compression = header[0x1E] + (header[0x1F]<<8) + (header[0x20]<<16) + (header[0x21]<<24),
nb_colors = header[0x2E] + (header[0x2F]<<8) + (header[0x30]<<16) + (header[0x31]<<24),
bpp = header[0x1C] + (header[0x1D]<<8);
if ((ulongT)file_size!=fsiz)
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid file_size %d specified in filename '%s' (expected %lu).",
cimg_instance,
file_size,filename?filename:"(FILE*)",fsiz);
if (header_size<0 || header_size>=file_size)
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid header size %d specified in filename '%s'.",
cimg_instance,
header_size,filename?filename:"(FILE*)");
if (offset<0 || offset>=file_size)
throw CImgIOException(_cimg_instance
"load_bmp(): Invalid offset %d specified in filename '%s'.",
cimg_instance,
offset,filename?filename:"(FILE*)");
if (header_size>40) cimg::fseek(nfile,header_size - 40,SEEK_CUR);
const intView on GitHub (pinned to f788b534b4)
Solutions
- Open the BMP in an editor/image tool and re-save so the header file_size is correct.
- Patch bytes 0x02-0x05 to the actual file size before loading.
- Use a more tolerant loader (img.load() may route to a different decoder, or decode via platform APIs).
- Check for truncation: compare real file size with expected size from the source of the file.
Example fix
// before img.load_bmp(path); // header file_size=0 // after std::fstream f(path, std::ios::binary|std::ios::in|std::ios::out); f.seekp(2); uint32_t sz = fileSize(path); f.write((char*)&sz,4); f.close(); img.load_bmp(path);
Defensive patterns
Strategy: try-catch
Validate before calling
bool bmpFileSizeFieldSane(const char* p){ std::ifstream f(p,std::ios::binary); unsigned char h[6]={0}; f.read((char*)h,6); uint32_t declared=h[2]|(h[3]<<8)|(h[4]<<16)|((uint32_t)h[5]<<24); std::ifstream e(p,std::ios::binary|std::ios::ate); uint32_t actual=(uint32_t)e.tellg(); return declared==actual; } Try / catch
try { img.load_bmp(path); } catch (cimg_library::CImgIOException& e) { /* header file_size mismatch: patch or transcode */ } Prevention
- Re-save BMPs from unknown encoders before loading
- Reject or repair files where the size field is 0
- Check downloads for truncation before loading
When it happens
Trigger: Loading a BMP whose header claims a size different from the real file length: headers written incorrectly by third-party tools, files modified after writing (appended/truncated), or files where the size field is 0 (some writers omit it).
Common situations: Images produced by embedded devices or custom encoders that set file_size=0; truncated or concatenated downloads; editing tools that rewrite pixel data without updating the header.
Related errors
- load_bmp(): Invalid header size %d specified in filename '%s
- load_bmp(): Specified filename is (null).
- load_bmp(): Invalid BMP file '%s'.
- load_bmp(): Invalid offset %d specified in filename '%s'.
- load_bmp(): File size %lu for filename '%s' does not match e
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/a5c1d25465acfb92.
Report an issue: GitHub.