2013-03-11 75 views
-2

當我在圖片框中加載一張圖片並且不首先點擊圖片框時,它會中斷。 當我把代碼返回到代碼中時,它會進入一個無限循環,當我刪除它時,代碼會中斷。如何處理:參數超出範圍?

這是代碼。它總是在第一點打破,我不知道如何處理它。

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"); 
} 
+0

你有沒有追蹤'clickedX'和'clickedY'的價值? – Raptor 2013-03-11 04:02:30

+0

我不知道如何 – 2013-03-11 16:46:22

+0

你可以使用'System.Diagnostics.Debug.WriteLine'打印出來的值 – Raptor 2013-03-12 02:16:38

回答

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); 

     } 

它更短,容易理解。 希望這會有用

+0

哥們我不想要2改變我的代碼我只需要2處理這個錯誤,只是謝謝任何方式 – 2013-03-11 04:26:52