[英]Out of range in Color.FromArgb(Int32,Int32,Int32)
I run this code in c# language ,but there is error that 我使用C#语言运行此代码,但是存在错误
"value of "257 is not valid for red.red should be greater than or equal to 0 and less than or equal to 255" “值“ 257对红色无效。红色应大于或等于0且小于或等于255”
How can I correct this error? 我该如何纠正该错误?
Int32[,] Y1= new Int32[width,height];//R,G,B not empty array
Int32[,] R= new Int32[width,height];
Int32[,] G= new Int32[width,height];
Int32[,] B= new Int32[width,height];
Bitmap bmp=new Bitmap[width,height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
Y1[x, y] = Convert.ToInt32(0.39 * R[x, y] + 0.59 * G[x, y] + 0.12 * B[x, y]);
bmp.SetPixel(x, y, Color.FromArgb(Y1[x,y],Y1[x,y],Y1[x,y]));
}
pictureBox1.Image = bmp;
I know that "Color.FromArgb(Y1[x,y],Y1[x,y],Y1[x,y])" out of range but how can I 我知道“ Color.FromArgb(Y1 [x,y],Y1 [x,y],Y1 [x,y])”超出范围,但是我该如何
correct it ? 纠正吗?
You're probably trying to convert to grayscale. 您可能正在尝试转换为灰度。 There are two things you should do: 您应该做两件事:
Apply a cap using Math.Min()
. 使用Math.Min()
上限。 Something like: 就像是:
Y1[x, y] = Math.Min(255, Convert.ToInt32(0.299 * R[x, y] + 0.587 * G[x, y] + 0.114 * B[x, y])); Y1 [x,y] = Math.Min(255,Convert.ToInt32(0.299 * R [x,y] + 0.587 * G [x,y] + 0.114 * B [x,y])));
The cap is there only to make sure our formula never exceeds the max byte value. 上限只是为了确保我们的公式不会超过最大字节值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.