Win2D库:CanvasImageBrush使用Canvas坐标绘制吗? [英] Win2D library: CanvasImageBrush paints using the Canvas coordinates?

查看:122
本文介绍了Win2D库:CanvasImageBrush使用Canvas坐标绘制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day乡亲,

我已经开始探索Windows 10 UAP应用程序的Win2D库.

I've started to explore the Win2D library for a Windows 10 UAP app.

我正在绘制一个椭圆,并使用先前定义的CanvasImageBrush对其进行填充.看起来,用作填充的坐标是基于绘制椭圆的屏幕坐标.

I am drawing an ellipse and filling it using a previously defined CanvasImageBrush. It appears the coordinates being used as fill are based on the screen coordinates where the ellipse is drawn.

换句话说,左上角的椭圆看起来不错,但是在页面的其他地方,椭圆填充为黑色.我将ExtendX和ExtendY属性设置为"Wrap"以检验我的假设,然后绘制椭圆,就好像它暴露了画笔使用的平铺图像一样.

In other words, an ellipse in the top left corner looks fine, but elsewhere on the page, the ellipse is filled with black. I set the ExtendX and ExtendY properties "Wrap" to test my hypothesis, and the ellipse is drawn as if it was exposing the tiled image used by the brush.

我附上了截图以显示行为.用于创建CanvasImageBrush的图像是一个包含三角形的162x148矩形.

I've attached a screenshot to show the behaviour. The image used to create the CanvasImageBrush is a 162x148 rectangle containing the triangles.

由于我无法想象这是预期的行为,所以我必须做一些愚蠢的事情.如果有人能指出什么,我将不胜感激.

Since I can't imagine this is expected behaviour, I must be doing something silly. I'd be grateful if someone could point out what.

这是代码

public sealed partial class MainPage : Page
{
    CanvasImageBrush fillBrush;
    List<Vector2> selectedPoints = new List<Vector2>();

    public MainPage()
    {
        this.InitializeComponent();
    }

    async Task CreateBrushes( CanvasControl sender )
    {
        CanvasBitmap cb = await CanvasBitmap.LoadAsync(sender, "Assets\\bitmapBrush.png");
        fillBrush = new CanvasImageBrush(sender, cb );
        fillBrush.ExtendX = CanvasEdgeBehavior.Wrap;
        fillBrush.ExtendY = CanvasEdgeBehavior.Wrap;
    }

    private void CanvasControl_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
    {
        args.TrackAsyncAction(CreateBrushes(sender).AsAsyncAction());
    }

    private void CanvasControl_Draw(CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
    {
        foreach( Vector2 v in selectedPoints )
        {
            args.DrawingSession.FillEllipse( v, 25, 25, fillBrush );
        }
    }

    private void MainCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        PointerPoint p = e.GetCurrentPoint((CanvasControl)sender);
        Vector2 v = new Vector2((float)p.Position.X, (float)p.Position.Y);

        selectedPoints.Add(v);

        MainCanvas.Invalidate();
    }

推荐答案

显然,这是预期的行为.

Apparently this is expected behaviour.

要使用CanvasImageBrush绘制一致的图像,需要使用.Transform方法将其转换为图像上的一致点.

To use a CanvasImageBrush to paint a consistent image, you need to use the .Transform method to translate to a consistent point on the image.

这篇关于Win2D库:CanvasImageBrush使用Canvas坐标绘制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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