1
    public void accessHi3DRange(buffer)
    {
        double meanR = 0;
        uint sumR = 0;
        uint countR = 0;
        int numberOfScansR = buffer.Height;
        bitmapHeight = numberOfScansR;

        int subCompWidth = buffer.Components["Hi3D 1"].SubComponents["Range"].Format.Width;
        bitmapWidth = subCompWidth;
        ushort[,] data = buffer.Components["Hi3D 1"].SubComponents["Range"].GetRows<ushort>(0, numberOfScansR);

        for (int scan = 0; scan < numberOfScansR; scan++)
        {

            for (int col = 0; col < subCompWidth; col++)
            {
                ushort val = data[scan, col];
                if (val != 0)
                {
                    sumR += val;
                    drawPix(col, scan, (int)val, (int)val, (int)val);
                    countR++;
                }
            }
        }
    }

    private void drawPix(int x, int y, int r, int g, int b)
    {
        ((Bitmap)pictureBox1.Image).SetPixel(x, y, Color.FromArgb(r, g, b));
        return;
    }

当我运行我得到的代码时

System.ArgumentException:'655' 的值对'red' 无效。'red' 应大于或等于 0 且小于或等于 255。

所以为了解决这个错误,我想将ushort值转换成byte,以便它可以显示在图片框中。如果是这样,我该如何转换?

4

1 回答 1

1

我认为问题在这里:

drawPix(col, scan, (int)val, (int)val, (int)val);

因为您将这些大ints 传递给Color.FromArgb(r, g, b)函数。MSDN 在这里说https://msdn.microsoft.com/en-us/library/cce5h557(v=vs.110).aspx R、G 和 B 的值必须是 0 到 255。

我认为您想要的是这种重载:https://msdn.microsoft.com/en-us/library/2zys7833(v=vs.110).aspx需要完整int然后将其从那里分解成a, r、g 和 b。

这需要对您的代码进行以下编辑:

 public void accessHi3DRange(buffer)
    {
        double meanR = 0;
        uint sumR = 0;
        uint countR = 0;
        int numberOfScansR = buffer.Height;
        bitmapHeight = numberOfScansR;

        int subCompWidth = buffer.Components["Hi3D 1"].SubComponents["Range"].Format.Width;
        bitmapWidth = subCompWidth;
        ushort[,] data = buffer.Components["Hi3D 1"].SubComponents["Range"].GetRows<ushort>(0, numberOfScansR);

        for (int scan = 0; scan < numberOfScansR; scan++)
        {

            for (int col = 0; col < subCompWidth; col++)
            {
                ushort val = data[scan, col];
                if (val != 0)
                {
                    sumR += val;
                    drawPix(col, scan, (int)val);
                    countR++;
                }
            }
        }
    }

    private void drawPix(int x, int y, int rgb)
    {
        ((Bitmap)pictureBox1.Image).SetPixel(x, y, Color.FromArgb(rgb));
        return;
    }
于 2017-12-13T04:24:36.827 回答