1

我有一个黑白线传感器,它给我扫描数据的灰度值(0-255)。我想将此值保存为位图(.bmp)并将它们显示在图片框中。我当前的代码如下所示:

        PixelFormat px = PixelFormat.Canonical;
        Bitmap bitmap = new Bitmap(width, height, px);
        int[,] GreyScaleArray2D = new int[width,height];

        for (int yy = 0; yy < (lb_SpReceivedData.Count-width); yy += width)
        {
            for (int xx = 0; xx < width; xx++)
            {
                GreyScaleArray2D[xx,tmp] = lb_SpReceivedData[(yy+xx)];
            }
            tmp++;
        }

        for (int y = 0; y < height; y++)
        {
           for (int x = 0; x < width; x++)
           {
              int red = GreyScaleArray2D[x,y]; // read from array
              int green = GreyScaleArray2D[x,y]; // read from array
              int blue = GreyScaleArray2D[x,y]; // read from array
              bitmap.SetPixel(x, y, Color.FromArgb(0, red, green, blue));
           }
        }

        pictureBox1.Image = bitmap; 
  1. 问题)如果我通过,我会收到无效参数的错误

         PixelFormat.Canonical
    

    到位图构造函数。

  2. 问题)即使我将位图传递给我的图片框,在图片框中也看不到任何东西!我究竟做错了什么?

谢谢

4

0 回答 0