WriteableBitmap和GestureListener_Tap的X,Y坐标不匹配-Windows Phone 8 [英] X,Y coordinates for WriteableBitmap and GestureListener_Tap do not match - Windows Phone 8

查看:102
本文介绍了WriteableBitmap和GestureListener_Tap的X,Y坐标不匹配-Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个隐藏的对象游戏,并试图在发现日食时标记该对象.我已经手动保存了通过GestureListener_Tap事件获得的每张图片的左上角和右下角坐标.

I am creating a hidden object game and am trying to mark the object when found with an eclipse. I've manually saved the top left and bottom right coordinates of each picture which were obtained via the GestureListener_Tap event.

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

The problem is when I tried to draw an eclipse bounded by the coordinates using this code

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

日食的位置始终位于左上方.使用以下代码标记像素位置,表明它们的位置确实与我从GestureListener_Tap期望的位置不同.

The location of the eclipse is always off to the top left. Marking the pixel locations using the following codes show that they are indeed located differently then what I would expect from the GestureListener_Tap.

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

我用于标记位置的代码:

My code for marking the location:

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

    }

我想念什么?

我在下面的回答未考虑屏幕方向更改.请参阅答案下方的我的评论.如何将像素坐标映射到图像坐标?

My answer below does not take into account the screen orientation changed. See my comment below the answer. How do you map pixel coordinates to image coordinate?

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

Found a correct solution. Updated my answer

推荐答案

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

From @PaulAnnetts comment I managed to transform the pixel coordinates. My inital mistake was to assume the image coordinates are the same as the pixel coordinates! I use the following code to convert.

    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事件中将像素转换为坐标.

Since PixelHeight and PixelWidth is fixed, and ActualHeight & ActualWidth isn't, I should convert pixel to coordinate in the GestureListerner_Tap event.

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

还有我的像素坐标转换器

And my pixel to coordinate converters

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

这篇关于WriteableBitmap和GestureListener_Tap的X,Y坐标不匹配-Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆