如何在 C# 中将 .bmp 文件转换为 1 和 0 的数组?

发布于 2025-01-15 00:52:12 字数 2086 浏览 0 评论 0原文

我有一个简单的黑色和白色 .bmp 文件,我想通过 C# 将其转换为一系列 0 和 1,不包括文件头和填充。 图像为 32x32 像素,这是我迄今为止尝试过的,但我无法删除填充和标题信息。

public BitArray imageToBinaryArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    bool[] arr = new bool[50000000];
    int i = 0;
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    BitArray bitarray = new BitArray(ms.ToArray());
    return bitarray;
}

private void readImage()
{
    BitArray data;
    string path = @"C:\Users\me\dummy.bmp";
    Image Dummy = Image.FromFile(path);
    data = imageToBinaryArray(Dummy);

    var sb = new StringBuilder();

    for (int i = 0; i < data.Count; i++)
    {
        char c = data[i] ? '1' : '0';
        sb.Append(c);
    }
    string s = sb.ToString();
    Console.WriteLine(s);
}

你可以在下面看到我要翻译的图。

虚拟 bmp

这就是我要打印的内容:

00000000000000000000000000000000
11111111111111111111111111111111
01010101010101010101010101010101
10101010101010101010101010101010
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
00000000000000000000000000000001
00000000000000000000000000000011
00000000000000000000000000000111
00000000000000000000000000001111
11111111111111111111111111111111
11110000000000000000000000000000
11100000000000000000000000000000
11000000000000000000000000000000
10000000000000000000000000000000
00000000000000000000000000000000
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111

提前感谢所有愿意帮助我的人!

I have a simple black & white .bmp file and i would like to convert it through C# into a series of 0s and 1s, excluding the file headers and padding.
The image is 32x32 pixels and this is what I have tried so far, but i couldn't remove the padding and header informations.

public BitArray imageToBinaryArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    bool[] arr = new bool[50000000];
    int i = 0;
    imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    BitArray bitarray = new BitArray(ms.ToArray());
    return bitarray;
}

private void readImage()
{
    BitArray data;
    string path = @"C:\Users\me\dummy.bmp";
    Image Dummy = Image.FromFile(path);
    data = imageToBinaryArray(Dummy);

    var sb = new StringBuilder();

    for (int i = 0; i < data.Count; i++)
    {
        char c = data[i] ? '1' : '0';
        sb.Append(c);
    }
    string s = sb.ToString();
    Console.WriteLine(s);
}

You can see below the figure i want to translate.

dummy bmp

This is what i want to print:

00000000000000000000000000000000
11111111111111111111111111111111
01010101010101010101010101010101
10101010101010101010101010101010
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
00000000000000000000000000000001
00000000000000000000000000000011
00000000000000000000000000000111
00000000000000000000000000001111
11111111111111111111111111111111
11110000000000000000000000000000
11100000000000000000000000000000
11000000000000000000000000000000
10000000000000000000000000000000
00000000000000000000000000000000
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111
11111111111111111111111111111111

Thanks in advance to everyone who will try to help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一城柳絮吹成雪 2025-01-22 00:52:12

尝试以下操作:

            byte[] input = {0x00, 0x00, 0x00, 0x00,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0x55, 0x55, 0x55, 0x55,
                            0xAA, 0xAA, 0xAA, 0xAA,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x01,
                            0x00, 0x00, 0x00, 0x03,
                            0x00, 0x00, 0x00, 0x0F,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xF0, 0x00, 0x00, 0x00,
                            0xE0, 0x00, 0x00, 0x00,
                            0xC0, 0x00, 0x00, 0x00,
                            0x80, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF
                           };
            string[] doubleWords = input.Select((x,i) => new {b = x, index = i})
                .GroupBy(x => x.index/4)
                .Select(x => string.Join("",x.Select(y => Convert.ToString(y.b, 2).PadLeft(8,'0'))))
                .ToArray();

Try following :

            byte[] input = {0x00, 0x00, 0x00, 0x00,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0x55, 0x55, 0x55, 0x55,
                            0xAA, 0xAA, 0xAA, 0xAA,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0x00, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x01,
                            0x00, 0x00, 0x00, 0x03,
                            0x00, 0x00, 0x00, 0x0F,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xF0, 0x00, 0x00, 0x00,
                            0xE0, 0x00, 0x00, 0x00,
                            0xC0, 0x00, 0x00, 0x00,
                            0x80, 0x00, 0x00, 0x00,
                            0x00, 0x00, 0x00, 0x00,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF,
                            0xFF, 0xFF, 0xFF, 0xFF
                           };
            string[] doubleWords = input.Select((x,i) => new {b = x, index = i})
                .GroupBy(x => x.index/4)
                .Select(x => string.Join("",x.Select(y => Convert.ToString(y.b, 2).PadLeft(8,'0'))))
                .ToArray();
深海里的那抹蓝 2025-01-22 00:52:12

正如此处所述,您可以将图像读入位图。然后迭代像素并将它们写入文本文件。

using System.Drawing;

Bitmap img = new Bitmap("*imagePath*");
for (int i = 0; i < img.Width; i++)
{
    for (int j = 0; j < img.Height; j++)
    {
        Color pixel = img.GetPixel(i,j);

        if (pixel == *somecondition*)
        {
            **Store pixel here in a array or list or whatever** 
        }
    }
} 

As explained here, you can read the image into a Bitmap. Then iterate through the pixels and write them to your textfile.

using System.Drawing;

Bitmap img = new Bitmap("*imagePath*");
for (int i = 0; i < img.Width; i++)
{
    for (int j = 0; j < img.Height; j++)
    {
        Color pixel = img.GetPixel(i,j);

        if (pixel == *somecondition*)
        {
            **Store pixel here in a array or list or whatever** 
        }
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文