gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:21:00 25 4
gpt4 key购买 nike

在我的隐藏对象游戏中,我想在使用以下代码找到对象时用圆形图像标记对象,其中 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();

我的问题是没有一个大圆圈,而是有几个小圆圈填充顶部的矩形区域(见图):

enter image description here

编辑 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();

这是结果

enter image description here

如果我设法解决这个问题,我将使用更大、质量更好的 circle.png。

最佳答案

首先我觉得circle.png太小了。 Blit 方法不会按比例放大。您需要先使用 Scale 函数将其放大,如下所示:

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

关于c# - 使用 WriteableBitmapEx Windows Phone 将较小的图像覆盖在较大的图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14671231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com