c# How to convert ushort value into byte so that I can display them in picturebox

495 views Asked by At
    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;
    }

When I run the code I get

System.ArgumentException: 'Value of '655' is not valid for 'red'. 'red' should be greater than or equal to 0 and less than or equal to 255.'

So to solve this error, am I suppose to convert ushort value into byte so that it can be displayed in picturebox. If so, how do I convert?

1

There are 1 answers

0
jwatts1980 On BEST ANSWER

I think the issue is here:

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

Because you are passing these large ints into theColor.FromArgb(r, g, b) function. MSDN says here https://msdn.microsoft.com/en-us/library/cce5h557(v=vs.110).aspx that the values for R, G, and B must be 0 to 255.

I think what you want is this overload: https://msdn.microsoft.com/en-us/library/2zys7833(v=vs.110).aspx that takes the full int and then breaks it apart from there into the a, r, g, and b.

Which would require the following edit to your code:

 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;
    }