简体   繁体   English

使用Win2D以比例长宽比调整图像大小

[英]Resizing an image using Win2D with proportional aspect ratio

I'm having an issue in my UWP Win2D app. 我的UWP Win2D应用程序出现问题。 I have an image and text being displayed in a CanvasAnimatedControl, and when I resize the window, it stretches both image and text instead of maintaining their proportions: 我有一个图像和文本显示在CanvasAnimatedControl中,当我调整窗口大小时,它会拉伸图像和文本,而不是保持它们的比例:

在此处输入图片说明

This is a portion of the code which loads and draws the image: 这是加载和绘制图像的代码的一部分:

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

The sample project can be downloaded here: 样本项目可以在这里下载:

PROJECT LINK 项目链接

What do I need to change so that the Image and Text don't stretch on resize?? 我需要更改什么以使图像和文本在调整大小时不会拉伸?

Found my solution, I use a ViewBox: 找到我的解决方案,我使用一个ViewBox:

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

I had to change the Stretch on it: 我不得不更改它的拉伸:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM