51

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

6 Answers 6

87

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

myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative));
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for being so recent. Also it's the only thing that worked for me too.
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)));
This probably doesn't answer the question, but since my question brought me here, this answer worked for me.
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...
Note That this and @Thought answer's doesn't work in WinUI 3. Because GetCurrentDirectory() return system32 directory. Instead You have to use this : loginLogo.Source = new BitmapImage(new Uri(new Uri(AppDomain.CurrentDomain.BaseDirectory, UriKind.Absolute), new Uri(@"Assets\Images\logo-adaptive-white.png", UriKind.Relative)));
79

You just need one line:

ImageViewer1.Source = new BitmapImage(new Uri(@"\myserver\folder1\Customer Data\sample.png"));

Comments

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");

Comments

5

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;
} 

Comments

0

My NET 7 project file looks as follows.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="images\RuntimePicture.png" />
  </ItemGroup>

  <ItemGroup>
    <Resource Include="images\RuntimePicture.png">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Resource>
  </ItemGroup>


</Project>

As the proj file indicates, I have an image in a folder called images. The code behind ctor looks as follows.

public MainWindow()
{
    InitializeComponent();
    ImageEx.Source = new BitmapImage(new Uri(@"/images/RuntimePicture.png", UriKind.Relative));

}

The main window xaml for image would be as follows.

<Image x:Name="ImageEx" Height="300" Width="500" />

Finally right click the png file and in the file properties window, ensure Build Action is set to Resource.

Comments

-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"));
    }

2 Comments

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.
Flippant remarks at the start of an answer are unnecessary. Full working answers, with directions on how to use are all that's required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.