如何在运行时将图像加载到WPF?
经过一些搜索,将运行时的图像加载到WPF窗口似乎相当复杂!
Image image; image = new Uri("Bilder/sas.png", UriKind.Relative); ????.Source = new BitmapImage(image);
我正在尝试使用此代码,但我需要一些帮助才能使其正常工作。我在代码下面有一些红线!我也想知道是否需要在XAML代码中添加一些额外的代码,或者是否足够了:
<Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="350" />
不知道,因为我看过XAML标签内部图像的示例。
编辑:
我知道这个:
var uri = new Uri("pack://application:,,,/sas.png"); var bitmap = new BitmapImage(uri); image1.Source = bitmap;
XAML:
<Grid Width="374"> <Image Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="350" /> <Button Content="Start" Height="23" HorizontalAlignment="Left" Margin="12,226,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" /> <Button Content="Land" Height="23" HorizontalAlignment="Left" Margin="287,226,0,0" Name="btnLand" VerticalAlignment="Top" Width="75" /> <ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" /></Grid>
编辑2:我的答案是“解决”,但在Stack Overflow之外的som帮助。这段代码工作正常:
BitmapImage image = new BitmapImage(); image.BeginInit(); image.UriSource = new Uri("pack://application:,,,/Resources/" + company + ".png"); image.EndInit(); image2.Source = image;
相关分类