【问题标题】:Decoding a .bmp file to binary将 .bmp 文件解码为二进制文件
【发布时间】:2011-12-01 17:55:30
【问题描述】:

我正在尝试编写一个旨在将 .bmp 文件转换为二进制文件的 C# 程序。 该文件为 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,请他们帮助我。也欢迎任何改进代码的方法。

【问题讨论】:

  • 您检查过 .GetPixel() 返回的内容吗?也许你在源图像中的“黑色”实际上有点非黑色,所以像素可能是 255,255,254 而 color.black 是 255,255,255。
  • 您到底为什么想要bools 数组中的数据? (只是好奇)
  • 至少有一个错误是您从 1 而不是 0 开始循环。

标签: c# bitmap barcode getpixel


【解决方案1】:

问题的根源可能是 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。这这样您以后就不必转换它了:)。

【讨论】:

    【解决方案2】:

    您可以使用此实用程序将位图转换为二进制文本。只需检查每个像素通道是否等于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;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      进行这些更改:

      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 吗?有什么接近的吗?

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-16
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多