C# WPF图像控制源
我正在尝试重新创建一个非常简单的C#project i WPF示例,它是一个简单的图像查看器。。从sam的自学C#中,我成功地打开了“打开文件”对话框,但是如何在WPF中设置image.source控件的映像路径C# WPF图像控制源,c#,image,wpf-controls,C#,Image,Wpf Controls,我正在尝试重新创建一个非常简单的C#project i WPF示例,它是一个简单的图像查看器。。从sam的自学C#中,我成功地打开了“打开文件”对话框,但是如何在WPF中设置image.source控件的映像路径 private void SearchBtn_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDia
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;
}
}
private void SearchBtn\u单击(对象发送者,路由目标)
{
Microsoft.Win32.OpenFileDialog openfile=新建Microsoft.Win32.OpenFileDialog();
openfile.DefaultExt=“*.jpg”;
openfile.Filter=“图像文件|*.jpg”;
Nullable result=openfile.ShowDialog();
如果(结果==真)
{
//imagebox.Source=openfile.FileName;
}
}
您需要将文件名更改为URI,然后创建位图图像
:
您还可以将图像添加为资源,即添加现有项并将图像的构建操作属性更改为资源 那么就这样引用它
BitmapImage bitImg = new BitmapImage();
bitImg.BeginInit();
bitImg.UriSource = new Uri("./Resource/Images/Bar1.png", UriKind.Relative);
bitImg.EndInit();
((Image)sender).Source = bitImg;
这样,您就不需要将图像包含在程序中,它作为资源捆绑在软件包中谢谢,您如何设置Sizemode.zoom属性?
imagebox.Source = new BitmapImage(new Uri(openfile.FileName));
BitmapImage bitImg = new BitmapImage();
bitImg.BeginInit();
bitImg.UriSource = new Uri("./Resource/Images/Bar1.png", UriKind.Relative);
bitImg.EndInit();
((Image)sender).Source = bitImg;