wpf 在画布内拉伸图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33020133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stretch Image inside Canvas
提问by user1121956
I would like to have Image inside of Canvas and fit it to the window. When canvas is empty it works fine (canvas is resized within the window), however when I add Image into it, canvas is not fitting to window anymore even though I have Stretch="Uniform" applied to the Image. I illustrate this behavior below. Using the canvas is requirement unfortunately because I draw shapes over it. Any ideas please?
我想在 Canvas 里面有 Image 并将其适合窗口。当画布为空时,它工作正常(画布在窗口内调整大小),但是当我将图像添加到其中时,即使我将 Stretch="Uniform" 应用于图像,画布也不再适合窗口。我在下面说明了这种行为。不幸的是,使用画布是必需的,因为我在它上面绘制了形状。请问有什么想法吗?
Good
好的
<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>
Bad
坏的
<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>
回答by Chris Persichetti
I don't think stretch works with canvas very well. See this answer: https://stackoverflow.com/a/6010270/93233
我不认为拉伸对画布效果很好。看到这个答案:https: //stackoverflow.com/a/6010270/93233
However I was able to get it to work with the following:
但是我能够让它与以下一起工作:
<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>


