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
C# Exercise Example
// 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);
}
}
}
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#.
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...
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...
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...
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 ...
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...
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 ...