WPF图像控制源
问题描述:
即时通讯尝试重新创建一个非常简单的C#项目示例我WPF,它的一个简单的图像查看器..从萨姆教自己C#,我设法让打开的文件对话框打开,但怎么做我将图像路径设置为WPF中的image.source控件?WPF图像控制源
private void SearchBtn_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
openfile.DefaultExt = "*.jpg";
openfile.Filter = "Image Files|*.jpg";
Nullable<bool> result = openfile.ShowDialog();
if (result == true)
{
//imagebox.Source = openfile.FileName;
}
}
答
imagebox.Source = new BitmapImage(new Uri(openfile.FileName));
答
,您需要更改文件名称为URI,然后创建一个BitmapImage的
:
if (File.Exists(openfile.FileName))
{
// Create image element to set as icon on the menu element
BitmapImage bmImage = new BitmapImage();
bmImage.BeginInit();
bmImage.UriSource = new Uri(openfile.FileName, UriKind.Absolute);
bmImage.EndInit();
// imagebox.Source = bmImage;
}
答
您还可以添加图片作为一种资源,即添加现有项目并将图像的构建操作属性更改为资源
然后以此方式引用它
BitmapImage bitImg = new BitmapImage();
bitImg.BeginInit();
bitImg.UriSource = new Uri("./Resource/Images/Bar1.png", UriKind.Relative);
bitImg.EndInit();
((Image)sender).Source = bitImg;
这样,你不需要包括与程序,它捆绑到包作为一个资源
谢谢你,你当如何设置Sizemode.zoom propertie的形象呢? – Dabiddo 2009-08-08 04:04:20
也适合我!谢谢 – Azeem 2012-04-26 17:13:30
谢谢。为我工作 – 2018-01-15 04:47:47