Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

BMP width & height, BinaryReader - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

BMP width & height, BinaryReader - Practice Exercises C# Sharp


Lesson 8:

File management


Exercise 8.21:

BMP width & height, BinaryReader


Objetive:

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

The structure of the header of a BMP file is:

File type (letters BM)
0-1

FileSize
2-5

Reserved
6-7

Reserved
8-9

Start of image data
10-13

Sizeofbitmapheader
14-17

Width (pixels)
18-21

Height (pixels)
22-25

Numberofplanes
26-27

Sizeofeachpoint
28-29

Compression(0=notcompressed)
30-33

Imagesize
34-37

Horizontal resolution
38-41

Verticalresolution
42-45

Sizeofcolortable
46-49

Importantcolorscounter
50-53


Source Code:


using System;
using System.IO;
public class BmpHeightWidth
{
    public static void Main()
    {
        BinaryReader myFile;
        byte b1, b2;
        int width, height;

        myFile = new BinaryReader(
            File.Open("example.bmp", FileMode.Open));
        b1 = myFile.ReadByte();
        b2 = myFile.ReadByte();

        if ((b1 == 0x42) && (b2 == 0x4D))
        {
            Console.WriteLine("It seems to be a BMP file");
            myFile.BaseStream.Seek(18, SeekOrigin.Begin);
            width = myFile.ReadInt32();
            height = myFile.ReadInt32();
            Console.WriteLine("Width: {0} pixels", width);
            Console.WriteLine("Height: {0} pixels", height);
        }
        else
            Console.WriteLine("It DOES NOT seem to be a BMP file");

        myFile.Close();
    }
}
Exercisey 8.21

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.