代码之家  ›  专栏  ›  技术社区  ›  Maximus

使用Win2D按比例纵横比调整图像大小

  •  0
  • Maximus  · 技术社区  · 8 年前

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

    enter image description here

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

    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

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

    1 回复  |  直到 8 年前
        1
  •  0
  •   Maximus    8 年前

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

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

    我不得不改变它的拉伸:

    <Viewbox Stretch="UniformToFill">
        <canvas:CanvasAnimatedControl x:Name="AnimatedControl" />
    </Viewbox>
    
    推荐文章