PCX Image File Checker in C#

This C# program checks if a given file is a valid PCX image file. If the file is a PCX image, the program reads the file's header to extract the width and height of the image. The width and height are calculated using the Xmin, Ymin, Xmax, and Ymax values found in the header. The program then displays the width and height of the image in the console. If the file is not a valid PCX image, the program outputs an error message.



Group

File Handling in C#

Objective

1. The program reads the file header of a PCX image.
2. It checks if the file is a valid PCX image by verifying the ID and version.
3.The program extracts the Xmin, Ymin, Xmax, and Ymax values from the header to compute the image width and height.
4. The width is calculated as Xmax - Xmin + 1, and the height as Ymax - Ymin + 1.
5. If the file is a valid PCX image, it will display the width and height of the image. Otherwise, it will output an error message.

Example usage:

checkPcxImage image.pcx


Create a program that checks if a file is a PCX image and, if so, displays its width and height using the given PCX format specifications.

Example C# Exercise

 Copy C# Code
using System;
using System.IO;

class PcxImageChecker
{
    // Main method where the program starts execution
    static void Main(string[] args)
    {
        // Check if the user provided a filename as a command line argument
        if (args.Length != 1)
        {
            Console.WriteLine("Usage: checkPcxImage ");
            return;
        }

        // Get the file path from the command line argument
        string fileName = args[0];

        // Try to read the file
        try
        {
            // Open the file for reading
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                // Read the first byte to check the file ID (should be 10 for PCX)
                byte id = (byte)fs.ReadByte();

                if (id != 10)  // 10 indicates that it's a valid PCX file
                {
                    Console.WriteLine("Not a valid PCX file.");
                    return;
                }

                // Skip version, encoding, and bits per pixel (we are only interested in the coordinates)
                fs.Seek(4, SeekOrigin.Begin);

                // Read Xmin, Ymin, Xmax, Ymax (4 bytes in total)
                byte[] coordinates = new byte[4];
                fs.Read(coordinates, 0, 4);

                // Extract Xmin, Ymin, Xmax, Ymax values from the byte array
                int Xmin = coordinates[0];
                int Ymin = coordinates[1];
                int Xmax = coordinates[2];
                int Ymax = coordinates[3];

                // Calculate the width and height of the image
                int width = Xmax - Xmin + 1;
                int height = Ymax - Ymin + 1;

                // Display the width and height of the image
                Console.WriteLine($"This is a valid PCX image.");
                Console.WriteLine($"Width: {width} pixels");
                Console.WriteLine($"Height: {height} pixels");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error reading the file: {ex.Message}");
        }
    }
}

 Output

//Case 1 - Valid PCX Image:

//For the command:
checkPcxImage image.pcx

//Output:
This is a valid PCX image.
Width: 100 pixels
Height: 200 pixels

//Case 2 - Invalid File (Not a PCX Image):

//For the command:
checkPcxImage invalidFile.txt

//Output:
Not a valid PCX file.

Share this C# Exercise

More C# Practice Exercises of File Handling in C#

Explore our set of C# Practice 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#.

  • Extract Alphabetic Characters from Binary File in C#

    This C# program extracts only the alphabetic characters from a binary file and dumps them into a separate file. The program identifies characters with ASCII codes between 32 and 12...

  • Convert C# Program to Pascal

    This C# program is designed to convert simple C# programs into Pascal language code. The program reads a C# source code file and translates basic constructs like if, for, while, va...

  • Hex Viewer Utility - Dump Utility in C#

    This C# program is a "dump" utility that functions as a hex viewer. It displays the contents of a given file in hexadecimal format, with 16 bytes in each row and 24 rows in each sc...

  • Display Fields in a DBF File in C#

    In this exercise, you will create a program that reads a DBF file and displays a list of fields contained within it. The DBF file format, used by the old dBase database manager, is...

  • Censor Text File Program in C#

    In this exercise, you will create a program that reads a text file, checks each word against a list of words to censor, and writes the modified text to a new file. The words to cen...

  • Extract Data from SQL INSERT Statements in C#

    In this exercise, you will create a C# program that parses SQL INSERT commands from a file and extracts their data in a structured format. The program will read SQL INSERT statemen...