Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if file is 24 bit BMP 2

Status
Not open for further replies.

theCroat

Technical User
Sep 27, 2006
20
HR
Hello i use common dialog to load file, i limited loading to .bmp files, however i'd like to limit users to load only 24 bit bmps

so for example when he tries to load non 24 bit file [8,16 or 32 bit] i want message box to popup and say msgbox"Please load 24 Bit file!", vbExclamanation

Thanks
 
The bitmap file begins with a BITMAPFILEHEADER structure followed by a BITMAPINFOHEADER structure. The bit-count of the bitmap is stored in the biBitCount member of the BITMAPINFOHEADER structure.

See the following code.
___
[tt]
Private Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Function GetBitmapBitCount(FileName As String) As Integer
Dim FN As Integer, bfh As BITMAPFILEHEADER, bih As BITMAPINFOHEADER
FN = FreeFile
Open FileName For Binary Access Read As #FN
Get #FN, , bfh
Get #FN, , bih
Close #FN
GetBitmapBitCount = bih.biBitCount
End Function

Private Sub Form_Load()
MsgBox GetBitmapBitCount("C:\WINDOWS\Coffee Bean.bmp")
End Sub[/tt]
___

Since you are only interested in reading the bit-count, you can also rewrite this function in a more concise (but obscure) way, which does not require any type declarations.
___
[tt]
Private Function GetBitmapBitCount(FileName As String) As Integer
Dim FN As Integer
FN = FreeFile
Open FileName For Binary Access Read As #FN
Get #FN, 29, GetBitmapBitCount
Close #FN
End Function[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top