c# 如何将 ushort 值转换为字节,以便我可以在图片框中显示它们
c# How to convert ushort value into byte so that I can display them in picturebox
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: 'Value of '655' is not valid for 'red'. 'red' should be greater than or equal to 0 and less than or equal to 255.'
所以为了解决这个错误,我想把ushort
值转换成byte
,这样它就可以在图片框中显示了。
如果是这样,我该如何转换?
我认为问题出在这里:
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;
}
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: 'Value of '655' is not valid for 'red'. 'red' should be greater than or equal to 0 and less than or equal to 255.'
所以为了解决这个错误,我想把ushort
值转换成byte
,这样它就可以在图片框中显示了。
如果是这样,我该如何转换?
我认为问题出在这里:
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;
}