Win2D library: CanvasImageBrush paints using the C

2019-09-04 05:14发布

问题:

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.