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

Lesson:

File Management


Exercise:

Display BPM on console


Objetive:

The Netpbm format is a family of image file formats designed with simplicity in mind, rather than small size. They can represent color, grayscale, or black and white images using plain text (even though a binary variant exists).

For example, a black and white image coded in ASCII is represented using the header "P1".

The following line (optional) might be a comment, preceded with #.

The next line contains the width and height of the image.

The remaining line(s) contain the data: 1 for black points and 0 for white points, as in this example:

P1

This is an example bitmap of the letter "J"
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0

(that would be the content of a file named "j.pbm").

Create a program to decode an image file like this and display it on the screen using only the console. Remember that the comment is optional.


Code:

Imports System
Imports System.IO
Class leerPBM
    Public Shared Sub Main(ByVal args As String())
        Dim fileName As String
        Dim data As String
        Dim line As String
        Dim width As Integer
        Dim height As Integer
        Console.WriteLine("Enter the file name: ")
        fileName = Console.ReadLine()
        If Not fileName.Contains(".pbm") Then fileName += ".pbm"

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

        Try
            Dim myFile As StreamReader = File.OpenText(fileName)
            line = myFile.ReadLine()

            If line <> "P1" Then
                Console.WriteLine("Does not seem a B&W ASCII PBM")
                Return
            End If

            Console.WriteLine("Found B&W ASCII PBM")
            line = myFile.ReadLine()

            If (line.Length > 1) AndAlso (line(0) = "#"c) Then
                Console.WriteLine("Comment: " & line.Substring(1))
                line = myFile.ReadLine()
            End If

            Dim widthheight As String() = line.Split(" "c)
            width = Convert.ToInt32(widthheight(0))
            height = Convert.ToInt32(widthheight(1))
            Console.WriteLine("width: {0}", width)
            Console.WriteLine("height: {0}", height)
            data = ""
            line = myFile.ReadLine()

            While line IsNot Nothing
                data += line
                line = myFile.ReadLine()
            End While

            myFile.Close()
        Catch problem As IOException
            Console.WriteLine("File was not read properly")
            Console.WriteLine("Details: {0}", problem.Message)
            Return
        End Try

        data = data.Replace(" ", "")
        Dim pos As Integer = 0

        For Each symbol As Char In data
            If pos Mod width = 0 Then Console.WriteLine()

            If symbol = "1"c Then
                Console.Write("X")
            ElseIf symbol = "0"c Then
                Console.Write(".")
            End If

            pos += 1
        Next

        Console.WriteLine()
    End Sub
End Class

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