Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / Visual Basic

Reading BMP File Type with Visual Basic

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
14 Jun 2010CPOL2 min read 50.1K   1.3K   5   1
This article just performs a way to read a file with Visual Basic 6.0 (VB)

Introduction

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:

VBScript
Type BITMAPFILE
     bmfh As BITMAPFILEHEADER
     bmih As BITMAPINFOHEADER
     aBitmapBits() As BGR  ‘ contain 3-byte elements
End Type

BITMAPFILEHEADER Structure

We can declare in VB:

VBScript
Type BITMAPFILEHEADER ‘14 bytes
     bfType As Integer
     bfSize As Long
     bfReserved1 As Integer
     bfReserved2 As Integer
     bfOffBits As Long
End Type

BITMAPINFOHEADER Structure

We declare:

VBScript
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
End Type

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.

VBScript
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

Please remember:

  1. Image data is stored in the last past of the *.bmp file.
  2. 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.
  3. 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:

Namepicture1
Appearance1 - 3D
AutoReDrawTrue
BorderStyle0 - None

Than add a CommandButton named cmdWork.

We define a variant called strVarPath, of which value is the path to the picture in *.bmp type.

Use this code:

VBScript
Dim bitmap1 As BITMAPFILE
Dim headers As Integer
Dim hei, wid, kx, ky As Long

Sub Init()
     Open strVarPath For Binary As #1
End Sub

Sub Read_header()
     Get #1, , bitmap1.bmfh
     Get #1, , bitmap1.bmih
     headers = LOF(1) - bitmap1.bmih.biSizeImage 	' size of the part from the
						' beginning to the data
End Sub

Sub Next_init()
Dim i, j As Long
Dim bitmp As String
     hei = bitmap1.bmih.biHeight
     wid = bitmap1.bmih.biWidth
     '--------------- get the height and width size
     kx = wid
     ky = hei
     '--------------- make the width even
     if (kx Mod 2 <> 0) then
          kx = kx + 1
     end if
     '--------------- declare an array, of which size = height x width
     ReDim bitmap1.aBitmapBits(ky, kx)
     Close #1
     Init
     bitmp = Space(headers)
     Get #1, , bitmp, '------ read the part before data
     For i = 1 To ky
          For j = 1 To kx
               Get #1, , bitmap1.aBitmapBits(i, j) ' here we read data line by line
          Next j
     Next i
End Sub

Sub process() '------ draw the data on picture1
Dim i, j As Long '------ image is drawn by drawing points, one by one on picture1
     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, _
               bitmap1.aBitmapBits(i, j).rgbBlue)
               Picture1.PSet (j, hei - i)
          Next j
     Next i
End Sub

Sub Finish()
     Close #1
End Sub

Private Sub cmdWork_Click()
     Init
     Read_header
     Next_init
     process
     Finish
End Sub

Conclusion

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.

History

  • 14th June, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Vietnam Vietnam
Oops!

Comments and Discussions

 
GeneralOdd size bmp Pin
kevenm22-Jun-10 2:24
professionalkevenm22-Jun-10 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.