Mega Code Archive

 
Categories / Delphi / Examples
 

Reading properties from a bitmap file [ bmp]

Question: My application needs to display properties (width, height, color depth) of bitmaps in a selected directory. How can I obtain such properties? Answer: Bitmap files have two headers of type TBitmapinfoheader. Simply open the file and read them as shown in the example below. procedure TForm1.Button1Click(Sender: TObject); var FileHeader: TBitmapfileheader; InfoHeader: TBitmapinfoheader; sBmpFile : TFilestream; begin { TForm1.Button1Click } sBmpFile := TFilestream.Create('C:\Bild.bmp', fmOpenRead); sBmpFile.Read(FileHeader, SizeOf(FileHeader)); sBmpFile.Read(InfoHeader, SizeOf(InfoHeader)); sBmpFile.Free; with ListBox1.Items do begin Clear; Add('File Size: ' + IntToStr(FileHeader.bfSize)); Add('Width: ' + IntToStr(InfoHeader.biWidth)); Add('Height: ' + IntToStr(InfoHeader.biHeight)); Add('Color Depth: ' + IntToStr(InfoHeader.biBitCount)); // bits per color // e.g. 8 means 2^8 = 256 colors // 24 = true color (16 Mio colors) end; { with ListBox1.Items } end; { TForm1.Button1Click }