Question

When I load a picture in the picturebox and don't click on the picturebox first it break. When I put the return back in the code it goes to an endless loop and when I remove it the code breaks.

Here is the code. It always breaks at point1 and I dont know how to handle it.

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");
}
Was it helpful?

Solution

You can calculate your points as follow:

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

And then calculate the sumRed, sumGreen and 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);

        }

It's more short and easy to understand. Hope this will be useful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top