6
Answers

image viewer for reading bitmap file by binary reading

tarun kumar

tarun kumar

15y
6.3k
1
after reading the algorithm of bitmap files i have created a small program which is not working properly but displaying zigzag pattern

my code is this

public void display(String filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);            
            if (br.ReadChar().ToString() + br.ReadChar().ToString() == "BM")
            {
                bfSize = br.ReadInt32();
                bfReserved1 = br.ReadInt16();
                bfReserved2 = br.ReadInt16();
                bfoffbits = br.ReadInt32();
                biSize = br.ReadInt32();
                biWidth = br.ReadInt32();
                biHeight = br.ReadInt32();
                biPlanes = br.ReadInt16();
                biBitCount = br.ReadInt16();
                biCompression = br.ReadInt32();
                biSizeImage = br.ReadInt32();
                biXPelsPerMeter = br.ReadInt32();
                biYPelsPerMeter = br.ReadInt32();
                biClrUsed = br.ReadInt32();
                biClrImportant = br.ReadInt32();                
                Bitmap bmp = new Bitmap((int)biWidth,(int)biHeight);
                pictureBox1.Size = new Size((int)biWidth, (int)biHeight);
                try
                {
                    for (int x = 0; x < biWidth ; x++)
                    {
                        for (int y = (int)biHeight-1; y >= 0; y--)
                        {
                            rgbRed=br.ReadByte();
                            rgbGreen=br.ReadByte();
                            rgbBlue = br.ReadByte();
                            bmp.SetPixel(x,y,Color.FromArgb(rgbRed,rgbGreen,rgbBlue));
                        }                           
                    }
                    pictureBox1.Image = bmp;                                  
                }
                catch
                {
                    MessageBox.Show("there is some problem with file format");
                }
            }
            else
                MessageBox.Show("this is not a bitmap file");
            fs.Flush();
        }


please help
Answers (6)