我用c#语言运行这段代码,但是有错误
"257的值无效,red.red应大于或等于0,小于或等于255“
如何纠正此错误?
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;我知道"Color.FromArgb(Y1x,y,Y1x,y,Y1x,y)“超出了范围,但我怎么能
纠正一下?
发布于 2014-01-12 13:19:04
你可能是想转换成灰阶。你应该做两件事:
Math.Min()应用一个上限。类似于:
Y1x,y= Math.Min(255,Convert.ToInt32(0.299 * Rx,y+ 0.587 * Gx,y+ 0.114 * Bx,y));上限的存在只是为了确保我们的公式不会超过最大字节值。
https://stackoverflow.com/questions/21075053
复制相似问题