47

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

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191

5 Answers5

80

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

myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative));
  • 4
    +1 for being so recent. Also it's the only thing that worked for me too. – Totumus Maximus Dec 11 '13 at 12:47
  • 5
    Note that this _doesn't_ work in UWP, because UWP seems to only accept Absolute URIs. Here's how I do it: `myImage.Source = new BitmapImage (new Uri (new Uri (Directory.GetCurrentDirectory(), UriKind.Absolute), new Uri (@"/Images/foo.png", UriKind.Relative)));` – Thought Apr 01 '17 at 23:30
  • This probably doesn't answer the question, but since my question brought me here, this answer worked for me. – Rachel Martin Dec 03 '18 at 20:08
  • The only problem I experience with this, is that the transparency of the png is lost, and there seems to be no way, to restore it... – frankenapps Feb 12 '20 at 20:31
77

You just need one line:

ImageViewer1.Source = new BitmapImage(new Uri(@"\myserver\folder1\Customer Data\sample.png"));
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
timtos
  • 2,225
  • 2
  • 27
  • 39
5

The pack syntax you are using here is for an image that is contained as a Resource within your application, not for a loose file in the file system.

You simply want to pass the actual path to the UriSource:

logo.UriSource = new Uri(@"\\myserver\folder1\Customer Data\sample.png");
Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92
4

None of the methods worked for me as i needed to pull the image from a folder instead of adding it to the application. The below code worked:

TestImage.Source = GetImage("/Content/Images/test.png")

private static BitmapImage GetImage(string imageUri)
{
    var bitmapImage = new BitmapImage();
    
    bitmapImage.BeginInit();
    bitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageUri,             UriKind.RelativeOrAbsolute);
    bitmapImage.EndInit();
    
    return bitmapImage;
} 
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
-4

You are all wrong! Why? Because all you need is this code to work:

(image View) / C# Img is : your Image box

Keep this as is, without change ("ms-appx:///) this is code not your app name Images is your folder in your project you can change it. dog.png is your file in your folder, as well as i do my folder 'Images' and file 'dog.png' So the uri is :"ms-appx:///Images/dog.png" and my code :


private void Button_Click(object sender, RoutedEventArgs e)
    {
         img.Source = new BitmapImage(new Uri("ms-appx:///Images/dog.png"));
    }
voluntaryist7
  • 115
  • 15
  • 1
    I'd say you're all wrong because none of you provided a complete working example. It's just assumed that the reader knows where in the code to place these snippets. – Alex Jansen Mar 30 '19 at 07:57
  • Flippant remarks at the start of an answer are unnecessary. Full working answers, with directions on how to use are all that's required. – JoeTomks Mar 18 '20 at 14:33