如何处理:参数超出范围?

时间:2013-03-11 03:56:47

标签: c# outofrangeexception

当我在图片框中加载图片并且不首先点击图片框时它会中断。 当我把回程放回到代码中时,它会进入无限循环,当我删除代码时,代码会中断。

这是代码。它始终在第1点断开,我不知道如何处理它。

private void buttonNoiseAvr_Click(object sender, EventArgs e)
{
    Point Zeko = new Point(25, 25);

    if (pictureBoxSourcePicture.Image == null)
    {
        MessageBox.Show("No picture loaded");
        return;
    }

    if (pictureBoxSourcePicture.Height != clickedY || pictureBoxSourcePicture.Width != clickedX)
    {
        MessageBox.Show("Adeekll");
        //return;
    }

    Color oPoint = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX, clickedY);

    Color point1 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX - 1, clickedY - 1);
    Color point2 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX, clickedY - 1);
    Color point3 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX + 1, clickedY - 1);
    Color point4 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX - 1, clickedY + 1);
    Color point5 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX, clickedY + 1);
    Color point6 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX + 1, clickedY + 1);
    Color point7 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX - 1, clickedY);
    Color point8 = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX + 1, clickedY);


    //int[] XOR = new int[] { oPoint.R };
    //int[] XOG = new int[] { oPoint.G };
    //int[] XOB = new int[] { oPoint.B };

    int[] XR = new int[] { point1.R, point2.R, point3.R, point4.R, point5.R, point6.R, point7.R, point8.R };
    int[] XG = new int[] { point1.G, point2.G, point3.G, point4.G, point5.G, point6.G, point7.G, point8.G };
    int[] XB = new int[] { point1.B, point2.B, point3.B, point4.B, point5.B, point6.B, point7.B, point8.B };

    double SumRed = 0;
    double SumGreen = 0;
    double SumBlue = 0;

    for (int i = 0; i < XR.Length; i++)
    {
        SumRed += (Math.Abs(oPoint.R - XR[i])) * (Math.Abs(oPoint.R - XR[i]));
        SumGreen += (Math.Abs(oPoint.G - XG[i])) * (Math.Abs(oPoint.G - XG[i]));
        SumBlue += (Math.Abs(oPoint.B - XB[i])) * (Math.Abs(oPoint.B - XB[i]));
    }

    MessageBox.Show("The Average Difference of Red is = "
            + Math.Sqrt(SumRed) + "\n"
            + "The Average Difference of Green is = "
            + Math.Sqrt(SumGreen) + "\n"
            + "The Average Difference of Blue is = "
            + Math.Sqrt(SumBlue) + "\n");
}

1 个答案:

答案 0 :(得分:4)

您可以按照以下方式计算积分:

        Color[] points = new Color[8];
        int[] xPos = { -1, 0, 1, -1, 0, 1, -1, 1 };
        int[] yPos = { -1, -1, -1, 1, 1, 1, 0, 0 };
        for (int i = 0; i < 8; i++)
           points[i] = ((Bitmap)pictureBoxSourcePicture.Image).GetPixel(clickedX + xPos[i], clickedY + yPos[i]);

然后计算sumRed,sumGreen和SumBlue:

        for (int i = 0; i < points.Length; i++)
        {

            SumRed += Math.Pow( (Math.Abs(oPoint.R - points[i].R)),2);
            SumGreen += Math.Pow((Math.Abs(oPoint.R - points[i].G)), 2);
            SumBlue += Math.Pow((Math.Abs(oPoint.R - points[i].B)), 2);

        }

它更简短易懂。 希望这会有用