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:&#39;&#39; 655&#39;的值不适用于红色&#39;。 &#39;红色&#39;应大于或等于0且小于或等于255。&#39;
因此,要解决此错误,我想将ushort
值转换为byte
,以便可以在图片框中显示。
如果是这样,我该如何转换?
答案 0 :(得分:1)
我认为问题在于:
drawPix(col, scan, (int)val, (int)val, (int)val);
因为您将这些较大的int
传递到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;
}