Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF图像控制源_C#_Image_Wpf Controls - Fatal编程技术网

C# WPF图像控制源

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

我正在尝试重新创建一个非常简单的C#project i WPF示例,它是一个简单的图像查看器。。从sam的自学C#中,我成功地打开了“打开文件”对话框,但是如何在WPF中设置image.source控件的映像路径

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;