'The Bitmap Info project demonstrates one means of obtaining from a bitmap file on the disk its image information without loading the actual bitmap.
'
'Within a bitmap file are 2 header groupings of information describing the bitmap image in that file. This data is contained in the file's BITMAPFILEHEADER and BITMAPINFOHEADER structures. By using the VB Type to recreate these structures, the details of the bitmap contained in the file can be obtained with a simple binary read of this data.
'(Note: due to the formatting of the code to allow pasting into VB, the strings used here will necessitate horizontal scrolling of the browser window on displays under 1024x768 full screen.)
'
'
' Form Code
'
'To a form add the controls as indicated in the illustration. To see a template of this form with control names substituted for the control captions, click here to open the image in a new window.
'
'The names of the description labels is unimportant, but the data returned from the routine below posts the information to the lblInfo() label array. Also on the form is an image control (Image1), with its Stretch property set to True, two command buttons (cmdSelect and cmdEnd), and another label to contain the selected filename and path (lblFileName). Finally, the routine needs a common dialog control (CMDialog1) added to the form.
'
'Paste the following code into the general declarations section of the form:'--------------------------------------------------------------------------------
Option Explicit Private Const CANCELERR = 32755
Private Const BI_RGB = 0&
Private Const BI_RLE8 = 1&
Private Const BI_RLE4 = 2&
Private Const BI_BITFIELDS = 3&
Private Type BITMAPINFOHEADER '40 bytes
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 Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Sub Form_Load()
'initialize the form controls
lblFileName = "Select a bitmap or RLE file to detail..."
lblInfo(0) = ""
lblInfo(1) = ""
lblInfo(2) = ""
lblInfo(3) = ""
lblInfo(4) = ""
lblInfo(5) = ""
lblInfo(6) = ""
lblInfo(7) = ""
'position the form
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2End Sub
Private Sub cmdEnd_Click() Unload MeEnd Sub
Private Sub cmdSelect_Click()
'create some working variables
Dim ff As Integer
Dim tmp As String
'create the variables to hold the bitmap info
Dim FileHeader As BITMAPFILEHEADER
Dim InfoHeader As BITMAPINFOHEADER On Error GoTo cmdSelect_FileErrorHandler
'show the common dialog
CMDialog1.CancelError = True
CMDialog1.ShowOpen
'display a rendition of the loaded bitmap
Image1 = LoadPicture((CMDialog1.filename))
Image1.ZOrder 1
'read the file header info
ff = FreeFile
Open CMDialog1.filename For Binary Access Read As #ff
Get #ff, , FileHeader
Get #ff, , InfoHeader
Close #ff
'display the file info
lblFileName = CMDialog1.filename
lblInfo(0) = InfoHeader.biWidth & " pixels"
lblInfo(1) = InfoHeader.biHeight & " pixels"
'select the appropriate string based on the value of biCompression
Select Case InfoHeader.biSizeImage
Case 0: tmp$ = "BI_RGB bitmap; size variable not filled in."
Case Else: tmp$ = Format$(InfoHeader.biSizeImage, "#,###,###") & " bytes"
End Select
lblInfo(2) = tmp$
lblInfo(3) = InfoHeader.biPlanes
lblInfo(4) = InfoHeader.biBitCount & " (" & 2 ^ InfoHeader.biBitCount & " colours)" 'select the appropriate string based on the value of biCompression
Select Case InfoHeader.biCompression
Case BI_RGB: tmp$ = "Uncompressed bitmap."
Case BI_RLE8: tmp$ = "Run-length encoded (RLE) format for bitmaps with 8 bits per pixel."
Case BI_RLE4: tmp$ = "Run-length encoded (RLE) format for bitmaps with 4 bits per pixel."
Case BI_BITFIELDS: tmp$ = "Uncompressed 16- or 32-bit-per-pixel format."
End Select
lblInfo(5) = tmp$ 'select the appropriate string based on the value of biClrUsed
Select Case InfoHeader.biClrUsed
Case 0:
tmp$ = "Bitmap uses the maximum number of colours corresponding to the"
tmp$ = tmp$ & " bits-per-pixel for the compression mode." Case Is <> 0 And InfoHeader.biBitCount = 16:
tmp$ = "The size of the colour table used to optimize performance"
tmp$ = tmp$ & "of Windows colour palettes is " & Str$(InfoHeader.biClrUsed)
End Select
lblInfo(6) = tmp$
'select the appropriate string based on the value of biClrImportant
Select Case InfoHeader.biClrImportant
Case 0:
tmp$ = "All " & 2 ^ InfoHeader.biBitCount & " colour"
tmp$ = tmp$ & " indices are considered important for displaying this bitmap."
Case Is <> 0
tmp$ = "The number of colours that are considered important for displaying"
tmp$ = tmp$ & " this bitmap are " & Str$(InfoHeader.biClrImportant)
End Select
lblInfo(7) = tmp$
Exit Sub'handle file errors or the user choosing cancel
cmdSelect_FileErrorHandler: If Err <> CANCELERR Then MsgBox Error$(Err), 48, "Image Info"
lblFileName = "No file was selected."End Sub
'
'Within a bitmap file are 2 header groupings of information describing the bitmap image in that file. This data is contained in the file's BITMAPFILEHEADER and BITMAPINFOHEADER structures. By using the VB Type to recreate these structures, the details of the bitmap contained in the file can be obtained with a simple binary read of this data.
'(Note: due to the formatting of the code to allow pasting into VB, the strings used here will necessitate horizontal scrolling of the browser window on displays under 1024x768 full screen.)
'
'
' Form Code
'
'To a form add the controls as indicated in the illustration. To see a template of this form with control names substituted for the control captions, click here to open the image in a new window.
'
'The names of the description labels is unimportant, but the data returned from the routine below posts the information to the lblInfo() label array. Also on the form is an image control (Image1), with its Stretch property set to True, two command buttons (cmdSelect and cmdEnd), and another label to contain the selected filename and path (lblFileName). Finally, the routine needs a common dialog control (CMDialog1) added to the form.
'
'Paste the following code into the general declarations section of the form:'--------------------------------------------------------------------------------
Option Explicit Private Const CANCELERR = 32755
Private Const BI_RGB = 0&
Private Const BI_RLE8 = 1&
Private Const BI_RLE4 = 2&
Private Const BI_BITFIELDS = 3&
Private Type BITMAPINFOHEADER '40 bytes
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 Type BITMAPFILEHEADER
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Sub Form_Load()
'initialize the form controls
lblFileName = "Select a bitmap or RLE file to detail..."
lblInfo(0) = ""
lblInfo(1) = ""
lblInfo(2) = ""
lblInfo(3) = ""
lblInfo(4) = ""
lblInfo(5) = ""
lblInfo(6) = ""
lblInfo(7) = ""
'position the form
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2End Sub
Private Sub cmdEnd_Click() Unload MeEnd Sub
Private Sub cmdSelect_Click()
'create some working variables
Dim ff As Integer
Dim tmp As String
'create the variables to hold the bitmap info
Dim FileHeader As BITMAPFILEHEADER
Dim InfoHeader As BITMAPINFOHEADER On Error GoTo cmdSelect_FileErrorHandler
'show the common dialog
CMDialog1.CancelError = True
CMDialog1.ShowOpen
'display a rendition of the loaded bitmap
Image1 = LoadPicture((CMDialog1.filename))
Image1.ZOrder 1
'read the file header info
ff = FreeFile
Open CMDialog1.filename For Binary Access Read As #ff
Get #ff, , FileHeader
Get #ff, , InfoHeader
Close #ff
'display the file info
lblFileName = CMDialog1.filename
lblInfo(0) = InfoHeader.biWidth & " pixels"
lblInfo(1) = InfoHeader.biHeight & " pixels"
'select the appropriate string based on the value of biCompression
Select Case InfoHeader.biSizeImage
Case 0: tmp$ = "BI_RGB bitmap; size variable not filled in."
Case Else: tmp$ = Format$(InfoHeader.biSizeImage, "#,###,###") & " bytes"
End Select
lblInfo(2) = tmp$
lblInfo(3) = InfoHeader.biPlanes
lblInfo(4) = InfoHeader.biBitCount & " (" & 2 ^ InfoHeader.biBitCount & " colours)" 'select the appropriate string based on the value of biCompression
Select Case InfoHeader.biCompression
Case BI_RGB: tmp$ = "Uncompressed bitmap."
Case BI_RLE8: tmp$ = "Run-length encoded (RLE) format for bitmaps with 8 bits per pixel."
Case BI_RLE4: tmp$ = "Run-length encoded (RLE) format for bitmaps with 4 bits per pixel."
Case BI_BITFIELDS: tmp$ = "Uncompressed 16- or 32-bit-per-pixel format."
End Select
lblInfo(5) = tmp$ 'select the appropriate string based on the value of biClrUsed
Select Case InfoHeader.biClrUsed
Case 0:
tmp$ = "Bitmap uses the maximum number of colours corresponding to the"
tmp$ = tmp$ & " bits-per-pixel for the compression mode." Case Is <> 0 And InfoHeader.biBitCount = 16:
tmp$ = "The size of the colour table used to optimize performance"
tmp$ = tmp$ & "of Windows colour palettes is " & Str$(InfoHeader.biClrUsed)
End Select
lblInfo(6) = tmp$
'select the appropriate string based on the value of biClrImportant
Select Case InfoHeader.biClrImportant
Case 0:
tmp$ = "All " & 2 ^ InfoHeader.biBitCount & " colour"
tmp$ = tmp$ & " indices are considered important for displaying this bitmap."
Case Is <> 0
tmp$ = "The number of colours that are considered important for displaying"
tmp$ = tmp$ & " this bitmap are " & Str$(InfoHeader.biClrImportant)
End Select
lblInfo(7) = tmp$
Exit Sub'handle file errors or the user choosing cancel
cmdSelect_FileErrorHandler: If Err <> CANCELERR Then MsgBox Error$(Err), 48, "Image Info"
lblFileName = "No file was selected."End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货