As we know, Microsoft Visual Basic 6.0 IDE supports the image file type *.bmp very well. So, writing your own code for processing *.bmp is not necessary. This article just performs a way to read a file with Visual Basic 6.0 (VB).
About Standard Bitmap File Type
*.bmp bitmap file type is the standard bitmap type in Windows OS from Windows 3.0. It is possible to use compression method to store data, but it seems not to be as effective as *.png modern type.
Here is the structure of *.bmp file type:
- One 14-byte variant in
BITMAPFILEHEADER structure: stored the common information of the file
- One 40-byte variant in
BITMAPINFOHEADER structure: stored the data information of the picture
- One array called
RGBQUAD: stored the colors appearing in the picture, 4 bytes per element of array
- One array stored the color, which is in BGR (Blue – Green – Red) order: 3 bytes per color
RGBQUAD just stored the appeared colors, so we don’t care much about it.
To make it easier, declare the
BITMAPFILE as follows:
bmfh As BITMAPFILEHEADER
bmih As BITMAPINFOHEADER
aBitmapBits() As BGR ‘ contain 3-byte elements
We can declare in VB:
Type BITMAPFILEHEADER ‘14 bytes
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
Type BITMAPINFOHEADER ‘40 bytes
biSize As Long
biWidth As Long
biHeight As Long
iplanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
Note: If you calculate according to
biBitCount, then the formula is
MaxNumberOfColor = 2^ biBitCount.
Windows allows these values: 1 (black/white); 4 (16 colors); 8 (256 colors); 24 (16.7 colors).
Blue – Green – Red (BGR) Type
The BGR color type contains 3 fields in order : blue element, green element, red element.
Type BGR ‘3 bytes / 1 variant
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
End Type ‘note that the fields are always in order blue -> green -> red
Notes About Image Data
- Image data is stored in the last past of the *.bmp file.
- The number of elements stored in 1 line is always even. If size in byte of the width is odd, there will be a value 0 added at the end of line.
- Data is stored upside down: first line stores the last line of.
Implement In Visual Basic
Open Microsoft VB 6 IDE and put all the codes above in a module. Then add a
picturebox into your main form. The properties of
picturebox are in the following table:
|1 - 3D
|0 - None
Than add a
We define a variant called
strVarPath, of which value is the path to the picture in *.bmp type.
Use this code:
Dim bitmap1 As BITMAPFILE
Dim headers As Integer
Dim hei, wid, kx, ky As Long
Open strVarPath For Binary As #1
Get #1, , bitmap1.bmfh
Get #1, , bitmap1.bmih
headers = LOF(1) - bitmap1.bmih.biSizeImage End Sub
Dim i, j As Long
Dim bitmp As String
hei = bitmap1.bmih.biHeight
wid = bitmap1.bmih.biWidth
kx = wid
ky = hei
if (kx Mod 2 <> 0) then
kx = kx + 1
ReDim bitmap1.aBitmapBits(ky, kx)
bitmp = Space(headers)
Get #1, , bitmp, For i = 1 To ky
For j = 1 To kx
Get #1, , bitmap1.aBitmapBits(i, j) Next j
Sub process() Dim i, j As Long Picture1.Height = hei
Picture1.Width = wid
For i = 1 To hei
For j = 1 To wid
Picture1.ForeColor = RGB(bitmap1.aBitmapBits(i, j).rgbRed, _
bitmap1.aBitmapBits(i, j).rgbGreen, _
Picture1.PSet (j, hei - i)
Private Sub cmdWork_Click()
Because of drawing points one by one, the method is ineffective about time (it costs nearly 1 second to process an 800x640 picture). This article is just to give you a look on how to read a file. To read and process a file more effectively, you should use another algorithm.
- 14th June, 2010: Initial post