DelphiFAQ Home Search:

Reading properties from a bitmap file (*.bmp)

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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!

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.