How to read bitmap file information #19

You can easily get information about bitmap files such the file size, image height & width, bit count and colors used.

The TBitmapFileHeader (tagBITMAPFILEHEADER - Windows API) record is declared in Windows.pas although it is not mentioned in Delphi help. The same situation is with the TBitmapInfoHeader (tagBITMAPINFOHEADER - Windows API) record.

tagBITMAPINFOHEADER Struct Reference - TBitmapInfoHeader
DWORD biSize Size of tagBITMAPINFOHEADER
LONG biWidth width of bitmap
LONG biHeight height of bitmap
WORD biPlanes 1
WORD biBitCount 1 (mono) or 4 (16 colors ) or 8 (256 colors) or 24 (16 Mil colors)
DWORD biCompression RLE COMPRESSION
DWORD biSizeImage Width x height
LONG biXPelsPerMeter  
LONG biYPelsPerMeter  
LONG biClrUsed Number of palettes used (if less than standard)
DWORD biClrImportant Number of important color
tagBITMAPFILEHEADER Struct Reference - TBitmapFileHeader
Word bfType $4d42 (i.e. 'BM')
DWORD bfSize Size of file
DWORD Reserved1 Reserved
UNIT Reserved2 Reserved
DWORD bfOffBits Byte location in the file which is first byte of image
var
  BitmapFileHeader: TBitmapFileHeader;
  BitmapInfoHeader: TBitmapInfoHeader;
  FileStream      : TFileStream;
Begin
  ...
  // The file stream to the file. You should change the file path :-)
  FileStream := TFileStream.Create('C:WindowsBubbles.bmp', fmOpenRead);
  try
    FileStream.Read(BitmapFileHeader, SizeOf(BitmapFileHeader));
    FileStream.Read(BitmapInfoHeader, SizeOf(BitmapInfoHeader));
  finally
    FileStream.Free;
  end;
  ...
end;
Author: Unknown
Added: 2007/06/02
Last updated: 2009/05/04