C#以二进制形式读取bmp图像

时间:2014-11-04 19:23:54

标签: c#

我不知道如何以二进制图像的形式读取图像。我一直在使用以下行,但它仍然以null读取。

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

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

这实际上取决于你想做什么。例如,如果要读入.bmp文件并将其显示在PictureBox中,则可以执行以下操作:

(假设有一个名为pictureBox1的PictureBox)

        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);
            }
        }

答案 1 :(得分:-1)

为什么不看看这个主题How to open image in C# and convert to binary,它似乎就像你要求的那样,但对于黑白图像,不过有一些方法可以可以帮助你。我希望你发现这有用或至少能引导你朝着正确的方向前进。

此致