加载图像C#-WPF [英] Loading an Image C#-WPF

查看:73
本文介绍了加载图像C#-WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Trying to display an image on either a stack panel or Image so I added both o see which I get to work, I also want to change images when buton is clicked
In my XAML
<StackPanel Height="518" HorizontalAlignment="Left" Margin="1196,58,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="417" />

<Image Height="202" HorizontalAlignment="Left" Margin="522,457,0,0" Name="imgPhoto" Stretch="Fill" VerticalAlignment="Top" Width="435" />

For button 1 (Image)
imgPhoto.Source = new BitmapImage(new Uri("c:\\Temp\test.jpg"));

for button 2 (stackanel)
Image imgPhoto = new Image();
imgPhoto.Source = new BitmapImage(new Uri("c:\\Temp\test.jpg"));
stackPanel1.Children.Add(imgPhoto);

None of these methods work.
Any help is greatly appreciated

推荐答案

请以以下格式更改代码.
Please change your code in the following format.
Image imgPhoto = new Image();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri("c:\\Temp\test.jpg");
bitmap.EndInit();
imgPhoto.Source = bitmap;



希望以上解决方案有帮助.



Hope the above solution helps.


这篇关于加载图像C#-WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆