tarun kumar

tarun kumar

  • NA
  • 14
  • 0

Bitmap File Creator or copy a bitmap file element wise

Jan 7 2010 4:06 PM
my code for that is
 String filename;
            OpenFileDialog openFile1 = new OpenFileDialog();
            openFile1.Filter = "BitMap|*.bmp";
            openFile1.InitialDirectory = @"c:\";
            if (openFile1.ShowDialog() == DialogResult.OK)
            {
                filename = openFile1.FileName;

                FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

                FileStream fs1 = new FileStream("c:\\new2.bmp", FileMode.Create);
                BinaryReader br = new BinaryReader(fs);
                BinaryWriter bw = new BinaryWriter(fs1);
                if (br.ReadChar().ToString() + br.ReadChar().ToString() == "BM")
                {
                    bw.Write('B');
                    bw.Write('M');
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt16());
                    bw.Write(br.ReadInt16());
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt32());
                    biWidth = br.ReadInt32();
                    biHeight = br.ReadInt32();
                    bw.Write(biWidth);
                    bw.Write(biHeight);
                    bw.Write(br.ReadInt16());
                    bw.Write(br.ReadInt16());
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt32());
                    bw.Write(br.ReadInt32());
                    rgbRed = 0;
                    rgbGreen = 0;
                    rgbBlue = 0;
                    try
                    {
                        int padding = biWidth % 4;
                        for (int y = biHeight - 1; y >= 0; y--)
                        {
                            for (int x = 0; x < biWidth; x++)
                            {
                                bw.Write(br.ReadByte());
                                bw.Write(br.ReadByte());
                                bw.Write(br.ReadByte());
                            }
                            fs.Seek(padding, SeekOrigin.Current);
                            for (int i = 0; i < padding; i++)
                                bw.Write(1);
                        }
                    }
                    catch
                    {
                        MessageBox.Show("there is some problem with file format");
                    }
                }
                else
                    MessageBox.Show("this is not a bitmap file");
                fs.Flush();
                MessageBox.Show("program complete");
            }
but starting it
file is created with same size but the file is not showing any image. so can anyone please tell me what can be the reason behind this?
and solve my problem