BMP Width And Height, Binaryreader - C# Programming Exercise

This C# exercise involves creating a program to display the width and height of a BMP file using a BinaryReader. The program should read the BMP file header and extract the information related to the width and height of the image. The BMP file has a specific header structure that contains several key positions with important information, such as the file type, file size, image data start, and image dimensions (width and height). This exercise is perfect for learning how to work with BinaryReader in C#, which is useful for manipulating binary files and processing data efficiently.

The BMP file header has several key positions containing important information about the image, such as the width and height. The program should read these positions using a BinaryReader to obtain the width and height information of the image. This exercise will help you understand how to read and extract data from a binary file and familiarize you with the internal structure of a BMP file.

Remember that the program should be able to read the BMP file, identify the key positions within the header, and correctly extract the width and height values. This exercise will also help you improve your skills in handling binary files and using tools like BinaryReader in C# to access specific data in files.

 Category

File Management

 Exercise

BMP Width And Height, Binaryreader

 Objective

Create a C# program to display the width and height of a BMP file using a BinaryReader.

The structure of the header of a BMP file is:

File type (letters BM) at positions 0-1.
File size at positions 2-5.
Reserved at positions 6-7.
Reserved at positions 8-9.
Start of image data at positions 10-13.
Size of bitmap header at positions 14-17.
Width (pixels) at positions 18-21.
Height (pixels) at positions 22-25.
Number of planes at positions 26-27.
Size of each point at positions 28-29.
Compression (0=not compressed) at positions 30-33.
Image size at positions 34-37.
Horizontal resolution at positions 38-41.
Vertical resolution at positions 42-45.
Size of color table at positions 46-49.

 Write Your C# Exercise

// Import the System namespace for basic functionality
using System;  

// Import the IO namespace for file handling
using System.IO;  

// Create the class to read BMP file header
class BMPReader
{
    // Main method where the program starts
    static void Main(string[] args)
    {
        // Ask the user for the BMP file path
        Console.WriteLine("Enter the path of the BMP file:");  

        // Get the file path from the user
        string filePath = Console.ReadLine();  

        // Start of try block to handle any file or reading errors
        try
        {
            // Open the BMP file with BinaryReader to read the binary data
            using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
            {
                // Check if the first two bytes are 'BM' to validate it as a BMP file
                if (reader.ReadByte() == 0x42 && reader.ReadByte() == 0x4D)
                {
                    // Skip the file size and reserved bytes (2 to 9)
                    reader.BaseStream.Seek(8, SeekOrigin.Begin);  

                    // Skip to the position where the width is stored (18-21)
                    int width = reader.ReadInt32();  

                    // Skip to the position where the height is stored (22-25)
                    int height = reader.ReadInt32();  

                    // Display the width and height of the BMP file
                    Console.WriteLine("Width: " + width + " pixels");
                    Console.WriteLine("Height: " + height + " pixels");
                }
                else
                {
                    // If the file is not a valid BMP file, inform the user
                    Console.WriteLine("The file is not a valid BMP file.");
                }
            }
        }
        catch (Exception ex)  // Catch any exceptions that may occur during file reading
        {
            // Print the exception message if an error occurs
            Console.WriteLine("An error occurred: " + ex.Message);  
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of File Management

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  TXT to HTML translator

    This C# exercise involves creating a "Text to HTML converter". The program should read a source text file and create an HTML file from its contents. The destination f...

  •  Invert binary file V2

    This C# exercise involves creating a program to "invert" a file using a FileStream. The program should create a file with the same name as the original file, b...

  •  BMP width & height, FileStream

    This C# exercise involves creating a program that reads a BMP file using a FileStream and displays its width and height. The BMP format has a spe...

  •  File copier

    This C# exercise involves creating a program that copies a source file to a destination file using a FileStream and a block size of 512 KB. The program should ...

  •  MP3 reader

    This C# exercise is about the ID3 specifications, which apply to any file or audiovisual container, but are primarily used with audio containers. There are three comp...

  •  C to C# converter

    This exercise consists of creating a program that converts simple C programs, such as the following one, into C#, ensuring that the resulting program compiles ...