wpf 自动调整图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27402606/
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
wpf resize image automatically
提问by bassplayer142
I've been banging my head for a few days trying to figure out why my controls don't want to resize.
几天来我一直在努力弄清楚为什么我的控件不想调整大小。
For instance below, I have background.png as the window background in a re-sizable window. The window successfully re-sizes and the background fills the entire window as required. But the image will not! Both images are both .png, they are both the same dpi, and both have the same resolution.
例如下面,我将 background.png 作为可重新调整大小的窗口中的窗口背景。窗口成功地重新调整大小并且背景根据需要填满整个窗口。但图像不会!两个图像都是 .png,它们都是相同的 dpi,并且都具有相同的分辨率。
The image in the ActuatorUC should line up directly with the background considering that it has transparent parts.
考虑到 ActuatorUC 中的图像具有透明部分,它应该直接与背景对齐。
MainWindow.xaml ->
MainWindow.xaml ->
<Window.Background>
<ImageBrush ImageSource="Resources/background.png"></ImageBrush>
</Window.Background>
<Grid Name="mainGrid">
<tc:ActuatorUC x:Name="act1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
ActuatorUC.xaml ->
ActuatorUC.xaml ->
<Canvas Name="canvas" >
</Canvas>
ActuatorUC.xaml.cs ->
ActuatorUC.xaml.cs ->
private void LoadImage(string imageName )
{
this.canvas.Children.Clear();
Image image = new Image();
string newImageName = "pack://application:,,,/Resources/" + imageName + ".png";
image.Source = (new ImageSourceConverter()).ConvertFromString(newImageName) as ImageSource;
image.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
image.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
this.canvas.Children.Add(image);
}
The dynamically added picture works, it shows up but will not resize with the parent canvas.
动态添加的图片有效,它显示但不会随着父画布调整大小。
I've done tons of searches but I just can't figure this out. I'm pretty sure it's something simple or some misunderstanding I have about wpf. Unfortunately, the images are proprietary so I cannot add them to this posting.
我已经进行了大量搜索,但我无法弄清楚这一点。我很确定这很简单,或者我对 wpf 有一些误解。不幸的是,这些图像是专有的,所以我不能将它们添加到这篇文章中。
Thanks in advance.
提前致谢。
回答by bassplayer142
Thanks to Clemens comment I fixed it as below is the answer.
感谢克莱门斯的评论,我修复了它,如下是答案。
Window.Xaml ->
Window.Xaml ->
<Window.Background>
<ImageBrush ImageSource="Resources/background.png"></ImageBrush>
</Window.Background>
<Grid Name="mainGrid">
<tc:ActuatorUC x:Name="act1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Grid>
ActuatorUC.Xaml ->
ActuatorUC.Xaml ->
<Grid Name="grid" >
</Grid>
ActuatorUc.Xaml.cs ->
ActuatorUc.Xaml.cs ->
private void LoadImage(string imageName )
{
this.canvas.Children.Clear();
Image image = new Image();
string newImageName = "pack://application:,,,/Resources/" + imageName + ".png";
image.Stretch = Stretch.Fill;
image.Source = (new ImageSourceConverter()).ConvertFromString(newImageName) as ImageSource;
this.canvas.Children.Add(image);
}

