简体   繁体   English

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

[英]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? 有人可以帮忙吗?

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: 例如,如果要读取.bmp文件并将其显示在PictureBox中,则可以执行以下操作:

(assumes there's a PictureBox named pictureBox1) (假设有一个名为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);
            }
        }

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. 您为什么不看一下这个主题, 如何在C#中打开图像并将其转换为二进制文件 ,似乎就像您要的那样,但是对于黑白图像,但是有些方法可以提供帮助您。 I hope you find this helpful or at least that lead you in the right direction. 我希望这对您有所帮助,或者至少可以引导您朝正确的方向发展。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM