Conor Shannon
Conor Shannon

Reputation: 459

C# Read in a bmp image as binary

I have no idea how to read an image in as a binary image. I have being using the following lines however it keeps reading in null.

FileStream inf = new FileStream(name.ToString(), FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryReader data = new BinaryReader(inf);

Can anyone help?

Upvotes: 1

Views: 4381

Answers (2)

m_a_s
m_a_s

Reputation: 240

It really depends on what you want to do. For example, if you want to read in a .bmp file and display it in a PictureBox, you can do something like the following:

(assumes there's a PictureBox named pictureBox1)

        Stream bmpStream = null;
        var ofd1 = new OpenFileDialog();
        if(ofd1.ShowDialog()==DialogResult.OK)
        {
            try
            {
                bmpStream = ofd1.OpenFile();
                if(bmpStream != null)
                {
                    using (bmpStream)
                    {
                        pictureBox1.Image = new Bitmap(bmpStream);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:  could not read file. " + ex.Message);
            }
        }

Upvotes: 1

Ricardo Lopez
Ricardo Lopez

Reputation: 11

why don't you take a look on this topic How to open image in C# and convert to binary,it seems to be like what you're asking for, but for image in black and white, however there are some methods that can help you. I hope you find this helpful or at least that lead you in the right direction.

Regards

Upvotes: 0

Related Questions