【问题标题】:Resizing an image using Win2D with proportional aspect ratio使用具有比例纵横比的 Win2D 调整图像大小
【发布时间】:2018-04-21 02:29:43
【问题描述】:

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

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

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

我需要进行哪些更改以使图像和文本在调整大小时不会拉伸??

【问题讨论】:

    标签: c# uwp win2d


    【解决方案1】:

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

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

    我不得不改变它的拉伸:

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

    【讨论】:

    • 我不能。我回答了自己的问题,但无法接受。你可以帮我做这件事吗?谢谢!
    猜你喜欢
    • 2017-08-10
    • 2011-04-27
    • 2013-06-26
    • 2012-04-15
    • 2012-05-01
    • 2013-09-09
    相关资源
    最近更新 更多