将 .bmp 文件解码为二进制

发布于 2024-12-18 19:33:37 字数 1000 浏览 4 评论 0原文

我正在尝试编写一个 C# 程序,旨在将 .bmp 文件转换为二进制文件。 文件大小为 16x16 像素。每个黑色像素代表二进制中的一个,因此数字 10 将是 █[]█[][][][][]

我遇到的问题是我的代码无法识别黑色像素,因此输出始终是零。

    public Bitmap imgToDecode;

    private void button2_Click(object sender, EventArgs e)
    {
        int i = (imgToDecode.Height * imgToDecode.Width);
        bool[] pixData = new bool[i];

        int p = 0;

        for (int k = 1; k < imgToDecode.Height; k++)
        {
            for (int m = 1; m < imgToDecode.Width; m++)
            {
                if (imgToDecode.GetPixel(m, k) == Color.Black)
                {
                    pixData[p] = true;
                }
                else
                {
                    pixData[p] = false;
                }
                p++;
            }
        }

        for (int n = 0; n < pixData.Length; n++)
        {
            textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
        }


    }

如果有人知道为什么输出为 0,请帮助我。任何改进代码的方法也将受到欢迎。

I am trying to write a C# program designed to convert a .bmp file into binary.
The file is 16x16 pixels. Each black pixel represents a one in binary, so the number 10 would be █[]█[][][][][]

The problem I am having is that my code is not recognizing the black pixels, so the output is always zero.

    public Bitmap imgToDecode;

    private void button2_Click(object sender, EventArgs e)
    {
        int i = (imgToDecode.Height * imgToDecode.Width);
        bool[] pixData = new bool[i];

        int p = 0;

        for (int k = 1; k < imgToDecode.Height; k++)
        {
            for (int m = 1; m < imgToDecode.Width; m++)
            {
                if (imgToDecode.GetPixel(m, k) == Color.Black)
                {
                    pixData[p] = true;
                }
                else
                {
                    pixData[p] = false;
                }
                p++;
            }
        }

        for (int n = 0; n < pixData.Length; n++)
        {
            textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
        }


    }

If anyone has an idea as to why the output is 0, could they please help me. Also any ways of improving the code would be welcomed.

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

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

发布评论

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

评论(3

小傻瓜 2024-12-25 19:33:37

问题的根源可能是 Color.Black 等于 Color.FromArgb(0, 0, 0)

解决方案可能是将行:更改

if (imgToDecode.GetPixel(m, k) == Color.Black)

为:

if (imgToDecode.GetPixel(m, k) == Color.FromArgb(0, 0, 0))

或者更好的是,声明一个包含 (0,0,0) 颜色的变量,然后在此 if 中使用它陈述。
因此,请在方法的开头执行以下操作:

 Color black = Color.FromArgb(0, 0, 0);

然后将 if 更改为:

if (imgToDecode.GetPixel(m, k) == black)

更新:
循环起始值似乎存在一些小问题。我已经更新了你的代码。

public Bitmap imgToDecode;

private void button2_Click(object sender, EventArgs e)
{
    textBox2.Text = "";

    Color black = Color.FromArgb(0, 0, 0);

    int i = (imgToDecode.Height * imgToDecode.Width);
    bool[] pixData = new bool[i];

    int p = 0;

    for (int k = 0; k < imgToDecode.Height; k++)
    {
        for (int m = 0; m < imgToDecode.Width; m++)
        {
            pixData[p] = (imgToDecode.GetPixel(m, k) == black);

            p++;
        }
    }

    for (int n = 0; n < pixData.Length; n++)
    {
        textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
    }
}

如果您不需要 pixData 数组来包含 bool 值,您可以将其更改为 int 并分配 1 或 0。这样您就不需要以后不必转换它:)。

The source of the problem is probably that Color.Black is not equal to Color.FromArgb(0, 0, 0).

The solution would probably be to change the line:

if (imgToDecode.GetPixel(m, k) == Color.Black)

to:

if (imgToDecode.GetPixel(m, k) == Color.FromArgb(0, 0, 0))

or even better, declare a variable containing the (0,0,0) color and then use it in this if statement.
So do something like:

 Color black = Color.FromArgb(0, 0, 0);

in the beginning of your method and then change if to:

if (imgToDecode.GetPixel(m, k) == black)

UPDATE:
There seemed to be some minor issues with loops start values. I've updated your code.

public Bitmap imgToDecode;

private void button2_Click(object sender, EventArgs e)
{
    textBox2.Text = "";

    Color black = Color.FromArgb(0, 0, 0);

    int i = (imgToDecode.Height * imgToDecode.Width);
    bool[] pixData = new bool[i];

    int p = 0;

    for (int k = 0; k < imgToDecode.Height; k++)
    {
        for (int m = 0; m < imgToDecode.Width; m++)
        {
            pixData[p] = (imgToDecode.GetPixel(m, k) == black);

            p++;
        }
    }

    for (int n = 0; n < pixData.Length; n++)
    {
        textBox2.Text = (textBox2.Text + (Convert.ToInt32(pixData[n])));
    }
}

If you don't need pixData array to contain bool values, you can change it to int and asign 1 or 0. This way you don't have to convert it later :).

土豪我们做朋友吧 2024-12-25 19:33:37

您可以使用此实用程序将位图转换为二进制文本。只需检查每个像素通道是否等于 0,然后返回 1(如果是黑色)或 0(如果是白色):

public static IEnumerable<int> ToBinary(this Bitmap imgToDecode)
{
    for (int k = 0; k < imgToDecode.Height; k++)
    {
        for (int m = 0; m < imgToDecode.Width; m++)
        {
            yield return (imgToDecode.GetPixel(m, k).R == 0 && 
                          imgToDecode.GetPixel(m, k).G == 0 && 
                          imgToDecode.GetPixel(m, k).B == 0) ? 1 : 0;
        }
    }
}

You could use this utility to convert your bitmap to binary text. Just check if every pixel channels is equal to 0 and then return 1 (if black) or 0 (if white):

public static IEnumerable<int> ToBinary(this Bitmap imgToDecode)
{
    for (int k = 0; k < imgToDecode.Height; k++)
    {
        for (int m = 0; m < imgToDecode.Width; m++)
        {
            yield return (imgToDecode.GetPixel(m, k).R == 0 && 
                          imgToDecode.GetPixel(m, k).G == 0 && 
                          imgToDecode.GetPixel(m, k).B == 0) ? 1 : 0;
        }
    }
}
若水般的淡然安静女子 2024-12-25 19:33:37

进行以下更改:

for (int m = 1; m < imgToDecode.Width; m++)
{
    Color col = imgToDecode.GetPixel(m, k);
    if (col  == Color.Black)   //<---- Put a breakpoint here and see what col is.
    {
        pixData[p] = true;
    }
    ...

当您在“col”处中断时,看看它是什么。是255,255,255吗?有什么接近的吗?

这至少可以让您看到“黑色”像素与“白色”像素是什么,并且您可以正确地打开它。

编辑:汉斯正确地指出,您需要在这些循环上从“0”而不是“1”开始。

Make these changes:

for (int m = 1; m < imgToDecode.Width; m++)
{
    Color col = imgToDecode.GetPixel(m, k);
    if (col  == Color.Black)   //<---- Put a breakpoint here and see what col is.
    {
        pixData[p] = true;
    }
    ...

When you break on 'col', see what it is. Is it 255,255,255? Something close?

This at least gets you to see what 'black' pixels vs. the 'white' pixels are, and you can properly switch on that.

EDIT: And Hans points out correctly that you need to start at '0' instead of '1' on those loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文