Вопрос

private void selectColor_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        xMove += e.DeltaManipulation.Translation.X;
        yMove += e.DeltaManipulation.Translation.Y;

        double xMax = 350;
        double yMax = 40;

        if (xMove < 0)
        {
            xMove = 0;
        }
        else if (xMove > xMax)
        {
            xMove = xMax;
        }

        if (yMove < 0)
        {
            yMove = 0;
        }
        else if (yMove > yMax)
        {
            yMove = yMax;
        }

        int x = Convert.ToInt32(xMove);
        int y = Convert.ToInt32(yMove);

        var writeableBmp = new WriteableBitmap(selectColor, null);
        var tempColor = writeableBmp.GetPixel(x, y);
        Brush imageColor = new SolidColorBrush(tempColor);

        txtBlockName.Foreground = imageColor;
    }

This function is for handling the manipulationDelta when i tap and drag inside the canvas called selectColor. yMove and xMove are 2 doubles that record the total movement. they are declared prio to the function. As the title states i get a IndexOutOfRangeException, and it points at x. I dont see how that is possible since i have set max/min values that are within the canvas. My canvas is exactly 350x40, so when x = 180 it shouldn't give me this error. I am a little confused right now, any help/advice will be appreciated.

Это было полезно?

Решение

Set the limits to

    double xMax = 349;
    double yMax = 39;

0 .. 349 = 350 pixels
0 .. 39 = 40 pixels

You are off-by-one when you set the xMove and yMove to the actual max values

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top