Click here to Skip to main content
13,349,539 members (49,063 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 4 Dec 2012

Reading a 16 Color 32x32 bitmap

, 6 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Reading a 16 color 32x32 bitmap file and displaying it in a picturebox.
Sample Image


This is my first post. I wrote this code when I was learning binary file I/O in C#. This code is not accurate because it displays an inverted image of a bitmap file. I hope this article will help someone learn C#. This code is not optimised, it is just for beginners learning to work with files. 


Bitmap file: This file contains the pixel data we want to read and display. Here is structure of a bitmap file specified in MSDN:

Structure	                         Corresponding bytes	   
BITMAPFILEHEADER	                     0x00 0x0D	   
BITMAPINFOHEADER	                     0x0E 0x35	   
RGBQUAD array	                         0x36 0x75	   
Color-index array	                     0x76 0x275

Color data is stored from the 0x76 byte.

BIT Manipulation

In bitmap 1 byte stores two colors, i.e., first 4 bits contain one color and the last 4 contain another color.

Now to extract to the color from the byte. We have to shift the byte. To extract left 4 bits from the byte, just right shift it by 4 bits.

Now we get the required value from the byte. To extract the right 4 bits from the byte, we should left shift it by 4 bits.

This byte does not give the desired value so we again right shift the 4 bits.

Now we get the required value in the byte.


First open Visual Studio and create a new Windows app in C#. In the form, add a picturebox and a button.

Now double click and write the following code:

private void button1_Click(object sender, EventArgs e)
    Color bitcolor; // create new color type variable which is used to store color 
                    // return by our getcolor method

    Bitmap image = new Bitmap(32, 32);//this creates a new bitmap image with 32x32 pixels

    // now opening our bitmap file.
    FileStream file = new FileStream("face.bmp", FileMode.Open, FileAccess.Read);

    //now seek file pointer to 0x76 byte because color index starts from here.
    file.Seek(0x76, 0);

    byte bhigh, blow; //bhigh will store the left 4 bits and blow will store right 4 bits.

    int tmp; // this is the temp variable which will be used to store byte return by
            // file.ReadByte() method

    /*  now for the loop since it is a 32x32 bitmap there are 32 row & 32 column of pixels
     * here first loop with i is for row & second loop j is for column
     * we start by reading first row then second , third so on till 31 row
     * * 
     * */ 

    for (int i = 0; i < 32; i++) // set row
        for (int j = 0; j < 32 ; j += 2) //now read first row and all its columns
                                        // since 1 byte contains two colors we are setting 
                                        // 2 pixels at a time 
            tmp = file.ReadByte();      // get first byte from color
            bhigh = (byte)(tmp >> 4); //now get left for bits by shifting int to right by 4 bits
            blow = (byte)(tmp << 4); // get right 4 bits by first left  shift 4
            blow = (byte)(blow >> 4); // now right shift by 4
            bitcolor = getcolor(bhigh); // get color representing left 4 bits
            //=== setting the pixels of bitmap
            image.SetPixel(j, i, bitcolor);    // e.g. first pixel (1,0)   
            bitcolor = getcolor(blow);  // get color representing right 4 bits
            //=== setting the pixels of bitmap
            image.SetPixel(j+1 ,i, bitcolor);  // e.g. second pixel (2,0)
    //now set the picture box with our image
    this.pictureBox1.Image = image;

Now the method which will help us to retrieve our color:

public Color getcolor(byte a) // this is the method which will return our 16 color 
{                            // configure your own color
    switch (a)
        case 0: return Color.Black;
        case 1: return Color.DarkRed;
        case 2: return Color.DarkGreen;
        case 3: return Color.Yellow;
        case 4: return Color.Blue;
        case 5: return Color.Cyan;
        case 6: return Color.DarkGray;
        case 7: return Color.LightGray;
        case 8: return Color.LightGreen;
        case 9: return Color.LightPink;
        case 10: return Color.HotPink;
        case 11: return Color.Silver;
        case 12: return Color.Gold;
        case 13: return Color.Turquoise;
        case 14: return Color.Violet;
        case 15: return Color.White;
        default: return Color.Coral; 
           // default is not required


I don't know why the image shows upside down. If you know, please tell me.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Abdias Software8-Dec-12 14:01
groupAbdias Software8-Dec-12 14:01 
GeneralMy vote of 5 Pin
souravdash5436-Dec-12 2:37
membersouravdash5436-Dec-12 2:37 
GeneralMy vote of 2 Pin
jfriedman5-Dec-12 16:02
memberjfriedman5-Dec-12 16:02 
GeneralRe: My vote of 2 Pin
hexgear6-Dec-12 1:57
memberhexgear6-Dec-12 1:57 
SuggestionWhy doing 2 operations when 1 is enough Pin
Pascal-785-Dec-12 2:36
memberPascal-785-Dec-12 2:36 
GeneralRe: Why doing 2 operations when 1 is enough Pin
hexgear6-Dec-12 2:01
memberhexgear6-Dec-12 2:01 
GeneralRe: Why doing 2 operations when 1 is enough Pin
Pascal-786-Dec-12 2:29
memberPascal-786-Dec-12 2:29 
GeneralRe: Why doing 2 operations when 1 is enough Pin
hexgear6-Dec-12 6:46
memberhexgear6-Dec-12 6:46 
GeneralMy vote of 5 Pin
souravdash5435-Dec-12 2:06
membersouravdash5435-Dec-12 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 6 Dec 2012
Article Copyright 2012 by hexgear
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid