BMP width and height, BinaryReader Learn programming Visual Basic (


File Management


BMP width and height, BinaryReader


Create a Visual Basic ( program to display the width and height of a BMP file using a BinaryReader.

The structure of the header of a BMP file is:

File type (letters BM) at positions 0-1.
File size at positions 2-5.
Reserved at positions 6-7.
Reserved at positions 8-9.
Start of image data at positions 10-13.
Size of bitmap header at positions 14-17.
Width (pixels) at positions 18-21.
Height (pixels) at positions 22-25.
Number of planes at positions 26-27.
Size of each point at positions 28-29.
Compression (0=not compressed) at positions 30-33.
Image size at positions 34-37.
Horizontal resolution at positions 38-41.
Vertical resolution at positions 42-45.
Size of color table at positions 46-49.


Imports System
Imports System.IO
Public Class BmpHeightWidth
    Public Shared Sub Main()
        Dim myFile As BinaryReader
        Dim b1, b2 As Byte
        Dim width, height As Integer
        myFile = New BinaryReader(File.Open("example.bmp", FileMode.Open))
        b1 = myFile.ReadByte()
        b2 = myFile.ReadByte()

        If (b1 = &H42) AndAlso (b2 = &H4D) Then
            Console.WriteLine("It seems to be a BMP file")
            myFile.BaseStream.Seek(18, SeekOrigin.Begin)
            width = myFile.ReadInt32()
            height = myFile.ReadInt32()
            Console.WriteLine("Width: {0} pixels", width)
            Console.WriteLine("Height: {0} pixels", height)
            Console.WriteLine("It DOES NOT seem to be a BMP file")
        End If

    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.