Display BMP on console V2 Learn programming Visual Basic (VB.net)


File Management


Display BMP on console V2


Create a program to display a 72x24 BMP file on the console. You must use the information in the BMP header (refer to the exercise of Feb. 7th). Pay attention to the field named "start of image data". After that position, you will find the pixels of the image. You can ignore the information about the color palette and draw an "X" when the color is 255, and a blank space if the color is different.

Note: You can create a test image with the following steps on Paint for Windows: Open Paint, create a new image, change its properties in the File menu so that it is a color image, width 72, height 24, and save as "256 color bitmap (BMP)".


Imports System
Imports System.IO
Public Class DisplayPGM
    Public Shared Sub Main(ByVal args As String())
        Dim inputFile As BinaryReader
        Dim fileName As String = ""

        If args.Length <> 1 Then
            Console.WriteLine("Please enter filename...")
            fileName = Console.ReadLine()
            fileName = args(0)
        End If

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

            inputFile = New BinaryReader(File.Open(fileName, FileMode.Open))
            Dim tag1 As Char = Convert.ToChar(inputFile.ReadByte())
            Dim tag2 As Char = Convert.ToChar(inputFile.ReadByte())
            Dim endOfLine As Byte = inputFile.ReadByte()

            If (tag1 <> "P"c) OrElse (tag2 <> "5"c) OrElse (endOfLine <> 10) Then
                Console.WriteLine("The file is not a PGM")
            End If

            Dim data As Byte = 0
            Dim sizeOfImage As String = ""

            While data <> 10
                data = inputFile.ReadByte()
                sizeOfImage += Convert.ToChar(data)
            End While

            Dim widthAndHeight As String() = sizeOfImage.Split(" "c)
            Dim width As Integer = Convert.ToInt32(widthAndHeight(0))
            Dim height As Integer = Convert.ToInt32(widthAndHeight(1))
            Dim maxIntensity1 As Char = Convert.ToChar(inputFile.ReadByte())
            Dim maxIntensity2 As Char = Convert.ToChar(inputFile.ReadByte())
            Dim maxIntensity3 As Char = Convert.ToChar(inputFile.ReadByte())
            Dim endOfLine1 As Byte = inputFile.ReadByte()

            If (maxIntensity1 <> "2"c) OrElse (maxIntensity2 <> "5"c) OrElse (maxIntensity3 <> "5"c) OrElse (endOfLine1 <> 10) Then
                Console.WriteLine("Must be 256 grey levels")
            End If

        Catch e As Exception
            Console.WriteLine("Error: {0}", e)
        End Try
    End Sub
End Class

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