使用具有比例纵横比的 Win2D 调整图像大小

Resizing an image using Win2D with proportional aspect ratio

我的 UWP Win2D 应用程序有问题。我在 CanvasAnimatedControl 中显示了图像和文本,当我调整 window 大小时,它会拉伸图像和文本而不是保持它们的比例:

这是加载和绘制图像的部分代码:

private void Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args)
{
    if (!IsLoadInProgress())
    {
        args.DrawingSession.DrawImage(_bitmap);
        DrawText(args, "...TESTING TEXT...");
    }
}

private void DrawText(CanvasAnimatedDrawEventArgs args, string text)
{
    using (var textFormat = new CanvasTextFormat()
    {
        FontSize = 96,
        FontFamily = "Segoe UI",
        FontWeight = FontWeights.Medium
    })
    {
        args.DrawingSession.DrawText(
            text,
            new Vector2(20, (float)_bitmap.Size.Height / 2),
            Colors.White,
            textFormat);
    }
}

private async Task SetImageAndSize()
{
    if (_imagePath != null)
    {
        _bitmap = await CanvasBitmap.LoadAsync(AnimatedControl, new Uri(_imagePath, UriKind.RelativeOrAbsolute));

        // Set the controls size according to image pixels to display for image
        AnimatedControl.Height = _bitmap.SizeInPixels.Height;
        AnimatedControl.Width = _bitmap.SizeInPixels.Width;
    }
}

示例工程可以在这里下载:

PROJECT LINK

我需要更改什么才能使图像和文本在调整大小时不会拉伸?

找到我的解决方案,我使用 ViewBox:

<Viewbox Stretch="Fill">
    <canvas:CanvasAnimatedControl x:Name="AnimatedControl" />
</Viewbox>

我不得不更改其上的拉伸:

<Viewbox Stretch="UniformToFill">
    <canvas:CanvasAnimatedControl x:Name="AnimatedControl" />
</Viewbox>