【问题标题】:Stretch Image inside Canvas在画布内拉伸图像
【发布时间】:2015-10-09 01:05:37
【问题描述】:

我想在 Canvas 中添加 Image 并将其放置在窗口中。当画布为空时,它可以正常工作(画布在窗口内调整大小),但是当我将图像添加到其中时,即使我将 Stretch="Uniform" 应用于图像,画布也不再适合窗口。我在下面说明了这种行为。不幸的是,使用画布是必需的,因为我在上面绘制形状。请问有什么想法吗?

<Window x:Class="ImageCropper.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <Border BorderThickness="3" BorderBrush="Red">
        <Canvas Background="Blue">
        </Canvas>
    </Border>
</Window>

不好

<Window x:Class="ImageCropper.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window3" Height="300" Width="300">
    <Border BorderThickness="3" BorderBrush="Red">
        <Canvas Background="Blue">
            <Image Source="asd.png" Stretch="Uniform" />
        </Canvas>
    </Border>
</Window>

【问题讨论】:

  • 您的图像大于区域,请将 Canvas 换成 ViewBox,或者因为它看起来只是用作背景,为什么不将其用作带有 ImageBrush 的 Border.Background?
  • 我尝试用 ViewBox 包装图像,但它没有帮助,Border 只是为了说明这个问题,我不正常使用它。不幸的是,图像不仅仅是背景。
  • 所以你放弃了 viewbox 的画布并将 viewbox stretch 设置为统一或统一填充?这应该可以正常工作,猜想我们可能需要一个更真实的例子来说明你想要完成的事情。
  • 不,我只是用 ViewBox 包装了 Image。我不能像我写的那样放弃画布 - 它是一个名为 SelectionCanvas 的画布,当用户移动鼠标时它会绘制矩形 - 只是一个选择工具(但该类中没有关于定位或调整大小的代码,所以它是不相关):)

标签: wpf xaml


【解决方案1】:

我认为拉伸不适用于画布。看到这个答案: https://stackoverflow.com/a/6010270/93233

但是我能够让它与以下内容一起使用:

<Border BorderThickness="3" BorderBrush="Red">
    <Canvas Background="Blue" Name="canvas1">
        <Image Source="asd.png" Width="{Binding Path=ActualWidth, ElementName=canvas1}" Height="{Binding Path=ActualHeight, ElementName=canvas1}" Stretch="Uniform"/>
    </Canvas>
</Border>

【讨论】:

  • 太好了,很多帖子都说这是不可能的。太棒了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 2019-05-12
  • 2011-07-18
  • 2015-10-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
相关资源
最近更新 更多