Ancho y alto BMP, FileStream Aprende programación con ejercicios Visual Basic (VB.net)

Lección:

Administración de Archivos


Ejercicio:

Ancho y alto BMP, FileStream 52


Objetivo:

Cree un programa de Visual Basic (VB.net) para mostrar el ancho y el alto de un archivo BMP mediante FileStream.

Recuerda la estructura de la cabecera:

File type (letters BM)
0-1

FileSize
2-5

Reserved
6-7

Reserved
8-9

Start of image data
10-13

Sizeofbitmapheader
14-17

Width (pixels)
18-21

Height (pixels)
22-25

Numberofplanes
26-27

Sizeofeachpoint
28-29

Compression(0=notcompressed)
30-33

Imagesize
34-37

Horizontal resolution
38-41

Verticalresolution
42-45

Sizeofcolortable
46-49

Importantcolorscounter
50-53


Código:

Imports System
Imports System.IO
Public Class exercise188
    Public Shared Sub Main()
        Dim myFile As FileStream
        Dim data As Byte()
        Dim width, height As Integer
        Const SIZE As Integer = 54
        Console.Write("Enter the name of file: ")
        Dim fileName As String = Console.ReadLine()

        If Not File.Exists(fileName) Then
            Console.WriteLine("The file not exists!!!")
            Return
        End If

        Try
            myFile = File.OpenRead(fileName)
            data = New Byte(53) {}
            myFile.Read(data, 0, SIZE)
            myFile.Close()
            Dim b1 As Byte = data(0)
            Dim b2 As Byte = data(1)

            If (Convert.ToChar(b1) <> "B"c) OrElse (Convert.ToChar(b2) <> "M"c) Then
                Console.WriteLine("This File is NOT a BMP file")
                Return
            End If

            width = data(18) + (data(19) * 256) + (data(20) * 256 * 256) + (data(21) * 256 * 256 * 256)
            height = data(22) + (data(23) * 256) + (data(24) * 256 * 256) + (data(25) * 256 * 256 * 256)
            Console.WriteLine("Width: {0} - Heigth: {1}", width, height)
        Catch e As Exception
            Console.WriteLine("Error: {0}!!!", e.message())
        End Try
    End Sub
End Class