Mostrar El Ancho Y La Altura De Un Archivo BMP Usando Filestream En C#

Este programa lee el encabezado de un archivo BMP (mapa de bits) y extrae el ancho y la altura de la imagen mediante FileStream. Utiliza la estructura del encabezado BMP, donde el ancho y la altura se ubican en posiciones de byte específicas del archivo. El programa mostrará el ancho y la altura de la imagen BMP en píxeles, ubicados en el encabezado del archivo en las posiciones 18-21 (ancho) y 22-25 (alto). El programa abre el archivo, lee los bytes correspondientes al encabezado y extrae los valores de ancho y alto. Esta información se muestra al usuario.

Grupo

Manejo de archivos en C#

Objectivo

1. Solicite al usuario la ruta del archivo de la imagen BMP.
2. Abra el archivo BMP con un FileStream en modo lectura.
3. Navegue hasta las posiciones de byte específicas en el archivo para obtener el ancho y el alto.
4. Convierta los valores de byte a enteros para representar el ancho y el alto.
5. Muestre los valores de ancho y alto en la consola.

Cree un programa en C# para mostrar el ancho y el alto de un archivo BMP con un FileStream.

Ejemplo de ejercicio en C#

 Copiar código C#
using System;
using System.IO;

class BMPInfoExtractor
{
    // Main method to run the program
    static void Main(string[] args)
    {
        // Prompt the user for the file path of the BMP image
        Console.WriteLine("Please enter the file path of the BMP image:");

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

        try
        {
            // Open the BMP file using FileStream in read mode
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                // Check if the file is a BMP file by reading the first two bytes (BM)
                byte[] fileType = new byte[2];
                fileStream.Read(fileType, 0, 2);

                // If the file is not a BMP, display an error message
                if (fileType[0] != 'B' || fileType[1] != 'M')
                {
                    Console.WriteLine("The file is not a valid BMP file.");
                    return;
                }

                // Move the file pointer to the width field (bytes 18-21)
                fileStream.Seek(18, SeekOrigin.Begin);

                // Read the width (4 bytes)
                byte[] widthBytes = new byte[4];
                fileStream.Read(widthBytes, 0, 4);

                // Convert the byte array to an integer representing the width
                int width = BitConverter.ToInt32(widthBytes, 0);

                // Move the file pointer to the height field (bytes 22-25)
                fileStream.Seek(22, SeekOrigin.Begin);

                // Read the height (4 bytes)
                byte[] heightBytes = new byte[4];
                fileStream.Read(heightBytes, 0, 4);

                // Convert the byte array to an integer representing the height
                int height = BitConverter.ToInt32(heightBytes, 0);

                // Display the width and height of the BMP image
                Console.WriteLine($"Width: {width} pixels");
                Console.WriteLine($"Height: {height} pixels");
            }
        }
        catch (Exception ex)
        {
            // Handle any errors that may occur while opening or reading the file
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}

 Output

Please enter the file path of the BMP image:
image.bmp
Width: 800 pixels
Height: 600 pixels

Comparte este ejercicio de C#