C# 使用WriteableBitmapEx Windows Phone将较小的图像覆盖在较大的图像上

C# 使用WriteableBitmapEx Windows Phone将较小的图像覆盖在较大的图像上,c#,windows-phone-8,blit,writeablebitmapex,C#,Windows Phone 8,Blit,Writeablebitmapex,在我的隐藏对象游戏中,我想在使用以下代码找到对象时用圆形图像标记该对象,其中AnsX1、AnsX2、AnsY1、AnsY2是对象位置的像素坐标。应根据像素坐标标记的对象大小调整圆形图像的大小 imgCat.Source = writeableBmp; WriteableBitmap wbCircle = new WriteableBitmap(AnsX2 - AnsX1, AnsY2 - AnsY1); wbCircle = new Writea

在我的隐藏对象游戏中,我想在使用以下代码找到对象时用圆形图像标记该对象,其中AnsX1、AnsX2、AnsY1、AnsY2是对象位置的像素坐标。应根据像素坐标标记的对象大小调整圆形图像的大小

        imgCat.Source = writeableBmp;

        WriteableBitmap wbCircle = new WriteableBitmap(AnsX2 - AnsX1, AnsY2 - AnsY1);
        wbCircle = new WriteableBitmap(0, 0).FromContent("Images/circle.png");

        //Just to make sure the boundary is correct so I draw the green rec around the object
        writeableBmp.DrawRectangle(AnsX1, AnsY1, AnsX2, AnsY2, Colors.Green);

        Rect sourceRect = new Rect(0, 0, writeableBmp.PixelWidth, writeableBmp.PixelHeight);
        Rect destRect = new Rect(AnsX1, AnsY1, wbCircle.PixelWidth, wbCircle.PixelHeight);

        writeableBmp.Blit(destRect, wbCircle, sourceRect);
        writeableBmp.Invalidate();
我的问题是没有一个大圆,而是有几个较小的圆填充顶部的矩形区域(见图):

编辑1: 根据@Rene的回复,我将代码改为

        imgCat.Source = writeableBmp;

        //Just to make sure the boundary is correct so I draw the green rec around the object
        writeableBmp.DrawRectangle(AnsX1, AnsY1, AnsX2, AnsY2, Colors.Green);
        WriteableBitmap wbCircle = new WriteableBitmap(0, 0).FromContent("Images/circle.png");
        wbCircle = wbCircle.Resize(AnsX2 - AnsX1, AnsY2 - AnsY1, WriteableBitmapExtensions.Interpolation.Bilinear);

        Rect sourceRect = new Rect(0, 0, writeableBmp.PixelWidth, writeableBmp.PixelHeight);
        Rect destRect = new Rect(AnsX1, AnsY1, AnsX2 - AnsX1, AnsY2 - AnsY1);

        writeableBmp.Blit(destRect, wbCircle, sourceRect);
        writeableBmp.Invalidate();
这是结果


如果我能解决这个问题,我会使用一个更大、质量更好的circle.png。

首先,我认为circle.png太小了。Blit方法不能按比例放大。您需要首先使用缩放功能将其放大,如下所示:

wbCircle = wbCircle.Resize(AnsX2 - AnsX1, AnsY2 - AnsY1, WriteableBitmapExtensions.Interpolation.Bilinear);
其次,sourceRect使用整个/目标位图的大小,而不是wbCircle/源位图的大小。应该是:

sourceRect = new Rect(0, 0, wbCircle.PixelWidth, wbCircle.PixelHeight);
如果圆太小且上比例太高,缩放可能会导致一些缩放瑕疵。如果您确实只需要一个简单的彩色圆圈,也可以使用DrawCircle方法:

writeableBmp.DrawCircle(AnsX1, AnsY1, AnsX2, AnsY2, Colors.Green);

谢谢你的回复。调整大小后,圆会变大,但仍无法填充整个矩形。我编辑了我的问题。你能上传一个样本项目吗。这将大大加快这一进程。但是为什么不简单地使用DrawCircle呢?OK将上传一个示例项目。我尝试了DrawCircle,但圆圈线似乎有点参差不齐(我画了五个不同大小的圆圈以产生更厚的画笔)。无论如何,我认为使用图像标记位置看起来会更好,因为我打算使用的图像不是一个简单的圆圈。:)好啊sourceRect使用整个/目标位图的大小,而不是wbCircle/源位图的大小。我在上面编辑了我的回复。