|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace cp_bitmap_reader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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 create 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 start 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 its all column
// since 1 byte contain two color 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 represinting left 4 bits
//=== setting the pixels of bitmap
image.SetPixel(j, i, bitcolor); // eg. first pixel (1,0)
bitcolor = getcolor(blow); // get color represinting right 4 bits
//=== setting the pixels of bitmap
image.SetPixel(j + 1, i, bitcolor); // eg. second pixel (2,0)
}
}
//now set the picture box with our image
this.pictureBox1.Image = image;
}
public Color getcolor(byte a) // this is the method which will return our 16 color
{ // configue your own color
switch (a)
{
case 0: return Color.Black;
break;
case 1: return Color.DarkRed;
break;
case 2: return Color.DarkGreen;
break;
case 3: return Color.Yellow;
break;
case 4: return Color.Blue;
break;
case 5: return Color.Cyan;
break;
case 6: return Color.DarkGray;
break;
case 7: return Color.LightGray;
break;
case 8: return Color.LightGreen;
break;
case 9: return Color.LightPink;
break;
case 10: return Color.HotPink;
break;
case 11: return Color.Silver;
break;
case 12: return Color.Gold;
break;
case 13: return Color.Turquoise;
break;
case 14: return Color.Violet;
break;
case 15: return Color.White;
break;
default: return Color.Coral; // default is not required
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.