DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows Graphics
Graphics programming under Windows with Delphi.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

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 } 
 
You don't like the formatting? Check out SourceCoder then!
Generated 4:02:07 on Aug 14, 2018