Reading a binary file (1: BMP) Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Reading a binary file (1: BMP) 66


Objetive:

Create a Visual Basic (VB.net) program to check if a BMP image file seems to be correct.

It must see if the first two bytes are B and M (ASCII codes 0x42 and 0x4D).


Code:

Imports System
Imports System.IO
Public Class BmpFile
    Public Shared Sub Main()
        Dim data1, data2 As Byte
        Dim myFile As BinaryReader
        myFile = New BinaryReader(File.Open("1.bmp", FileMode.Open))
        data1 = myFile.ReadByte()
        data2 = myFile.ReadByte()
        myFile.Close()

        If (data1 = &H42) AndAlso (data2 = &H4D) Then
            Console.WriteLine("It seems to be a BMP file")
        Else
            Console.WriteLine("It DOES NOT seem to be a BMP file")
        End If
    End Sub
End Class