【问题标题】:X,Y coordinates for WriteableBitmap and GestureListener_Tap do not match - Windows Phone 8WriteableBitmap 和 GestureListener_Tap 的 X、Y 坐标不匹配 - Windows Phone 8
【发布时间】:2013-02-02 07:08:37
【问题描述】:

我正在创建一个隐藏对象游戏,并尝试在使用 Eclipse 找到该对象时对其进行标记。我已经手动保存了通过 GestureListener_Tap 事件获得的每张图片的左上角和右下角坐标。

问题是当我尝试使用此代码绘制以坐标为界的日食时

WriteableBitmapExtensions.DrawEllipse(writeableBmp, AnsX1, AnsY1, AnsX2, AnsY2, Colors.Red);

日食的位置总是在左上角。使用以下代码标记像素位置表明它们的位置确实与我对 GestureListener_Tap 的期望不同。

writeableBmp.SetPixel(AnsX1, AnsY1, Colors.Red);
writeableBmp.SetPixel(AnsX2, AnsY2, Colors.Red);

我的位置标记代码:

    private void fadeOutAnimation_Ended(object sender, EventArgs e)
    {

        WriteableBitmap writeableBmp = new WriteableBitmap(bmpCurrent);
        imgCat.Source = writeableBmp;
        writeableBmp.GetBitmapContext();

        WriteableBitmapExtensions.DrawEllipse(writeableBmp, AnsX1, AnsY1, AnsX2, AnsY2, Colors.Red);
        writeableBmp.SetPixel(AnsX1, AnsY1, Colors.Red);
        writeableBmp.SetPixel(AnsX2, AnsY2, Colors.Red);

        // Present the WriteableBitmap
        writeableBmp.Invalidate();       

        //Just some animation code
        RadFadeAnimation fadeInAnimation = new RadFadeAnimation();
        fadeInAnimation.StartOpacity = 0.2;
        fadeInAnimation.EndOpacity = 1.0;
        RadAnimationManager.Play(this.imgCat, fadeInAnimation);

    }

我错过了什么?

编辑:

我在下面的回答没有考虑屏幕方向的变化。请参阅我在答案下方的评论。如何将像素坐标映射到图像坐标?

编辑 2:

找到了正确的解决方案。更新了我的答案

【问题讨论】:

  • 你为什么使用 GestureListener 而不是 WriteableBitmap 上的内置 Tap 事件?这可能会阻止您转换像素坐标。
  • GestureListener 从一开始就被使用,因为我想要放大/缩小和拖动图像的能力。如何转换像素坐标?
  • 没关系。我想到了。谢谢:)

标签: c# drawing windows-phone-8 writeablebitmap


【解决方案1】:

根据@PaulAnnetts 的评论,我设法转换了像素坐标。我最初的错误是假设图像坐标与像素坐标相同!我使用以下代码进行转换。

    private int xCoordinateToPixel(int coordinate)
    {
        double x;
        x = writeableBmp.PixelWidth / imgCat.ActualWidth * coordinate;
        return Convert.ToInt32(x);
    }

    private int yCoordinateToPixel(int coordinate)
    {
        double y;
        y = writeableBmp.PixelHeight / imgCat.ActualHeight * coordinate;
        return Convert.ToInt32(y);
    }

编辑:

由于 PixelHeight 和 PixelWidth 是固定的,而 ActualHeight 和 ActualWidth 不是,我应该在 GestureListerner_Tap 事件中将像素转换为坐标。

        if ((X >= xPixelToCoordinate(AnsX1) && Y >= yPixelToCoordinate(AnsY1)) && (X <= xPixelToCoordinate(AnsX2) && Y <= yPixelToCoordinate(AnsY2)))
        {...}

和我的像素坐标转换器

    private int xPixelToCoordinate(int xpixel)
    {
        double x = imgCat.ActualWidth / writeableBmp.PixelWidth * xpixel;

        return Convert.ToInt32(x);
    }

    private int yPixelToCoordinate(int ypixel)
    {
        double y = imgCat.ActualHeight / writeableBmp.PixelHeight * ypixel;
        return Convert.ToInt32(y);
    }

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    相关资源
    最近更新 更多