Win2D library: CanvasImageBrush paints using the Canvas coordinates?

孤人 提交于 2020-01-07 02:39:09

问题


G'day Folks,

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

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.

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.

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.

Here is the code

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

回答1:


Apparently this is expected behaviour.

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.



来源:https://stackoverflow.com/questions/35730646/win2d-library-canvasimagebrush-paints-using-the-canvas-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!