Reading a binay file (2 - GIF) Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Reading a binay file (2 - GIF) 62


Objetive:

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

It must see if the first four bytes are G, I, F, 8.

In case it seems correct, it must also display the GIF version (87 or 89), checking if the following byte is a 7 or a 9.


Code:

Imports System
Imports System.IO
Public Class GifFile
    Public Shared Sub Main()
        Dim data As Byte() = New Byte(4) {}
        Dim file As BinaryReader = New BinaryReader(File.Open("test.gif", FileMode.Open))

        For i As Integer = 0 To 5 - 1
            data(i) = file.ReadByte()
        Next

        file.Close()

        If data(0) = Convert.ToByte("G"c) AndAlso data(1) = Convert.ToByte("I"c) AndAlso data(2) = Convert.ToByte("F"c) AndAlso data(3) = Convert.ToByte("8"c) Then
            Console.WriteLine("Its a GIF8" & data(4))
        Else
            Console.WriteLine("It not gif file")
        End If
    End Sub
End Class