在后面的代码中更改图像源 - Wpf

Muh*_*tar 37 c# wpf imagesource

我需要动态设置图像源,请注意我的图像位于网络上的某个位置,这是我的代码

BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(@"pack://application:,,,\\myserver\\folder1\\Customer Data\\sample.png");
logo.EndInit(); // Getting the exception here
ImageViewer1.Source = logo;
Run Code Online (Sandbox Code Playgroud)

例外:

无法识别URI前缀

Man*_*ana 65

上述解决方案均不适合我.但这样做:

myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative));
Run Code Online (Sandbox Code Playgroud)

  • 最新的+1.它也是唯一对我有用的东西. (4认同)
  • 请注意,这个_不能在UWP中工作,因为UWP似乎只接受绝对URI.我是这样做的:`myImage.Source = new BitmapImage(new Uri(new Uri(Directory.GetCurrentDirectory(),UriKind.Absolute),new Uri(@"/ Images/foo.png",UriKind.Relative))) ;` (2认同)

tim*_*tos 63

你只需要一行:

ImageViewer1.Source = new BitmapImage(new Uri(@"\myserver\folder1\Customer Data\sample.png"));
Run Code Online (Sandbox Code Playgroud)


Won*_*ane 5

您在此处使用的包语法是针对在应用程序中包含为资源的图像,而不是文件系统中的松散文件.

您只想将实际路径传递给UriSource:

logo.UriSource = new Uri(@"\\myserver\folder1\Customer Data\sample.png");
Run Code Online (Sandbox Code Playgroud)