Change image source in code behind - Wpf

Muhammad Akhtar picture Muhammad Akhtar · Sep 24, 2010 · Viewed 168.5k times · Source

I need to set image source dynamically, please note my image is in somewhere on the Network, here is my code

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;

Exception:

The URI prefix is not recognized

Answer

Manindra Moharana picture Manindra Moharana · Dec 9, 2013

None of the above solutions worked for me. But this did:

myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative));