15

I'm having problems setting the source for images in my Wpf application. I have an Image where the source is bound to the SourceUri property of the DataContext object, like this:

<Image Source="{Binding SourceUri}"></Image>

Now, I don't know what to set on the SourceUri property of my object. Setting the complete absolute path ("c:/etc/image.jpg") it displays nicely, but obviously I want to set the relative path instead. My images are stored in a folder Resources which is in the same folder as my application folder. In the end these images might come from anywhere, so adding them to the project really isn't an option.

I've tried the path relative to the application folder, and relative to the working path (debug-folder). Also tried using the "pack://.." syntax without luck, but read that this would not be any point doing.

Any hints on what I should try?

4 Answers 4

18

There's a handy method in System.IO.Path which can help with this:

return Path.GetFullPath("Resources/image.jpg");

This should return 'C:\Folders\MoreFolders\Resources\image.jpg' or whatever the full path is in your context. It will use the current working folder as the starting point.

Link to MSDN documentation on GetFullPath.

Sign up to request clarification or add additional context in comments.

1 Comment

This is great - it still works if I want to supply my own full path.
10

Perhaps you could make the SourceUri property of your DataContext object a bit cleverer, and determine what the application folder is, and return an absolute path based on that. For example:

public string SourceUri
{
    get
    {
        return Path.Combine(GetApplicationFolder(), "Resources/image.jpg");
    }
}

3 Comments

Sure - that works - Thx! Couldn't find a good way to find the application folder though, so feels a bit hacky at the moment.. Is there a GetApplicationFolder() in .Net? Couldn't find one.. But shouldn't relative path references work somehow? And these will then be relative to the application root folder? No?
try Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
That gives me the same as the one I'm using, "Path.GetFullPath("."), but I guess the latter depends on the working folder. So going with your suggestion. I got some more issues around this, but I'll figure it out.. Thx for your attention!
9

After some frustrating times trying with

 <Image Source="pack://application:,,,/{Binding ChannelInfo/ChannelImage}">

and

 <Image Source="pack://siteoforigin:,,,/{Binding ChannelInfo/ChannelImage}">

and

 <Image Source="/{Binding ChannelInfo/ChannelImage}">

I solved this implementing my own converter:

C# side:

public class MyImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string path= (string)value;

        try
        {
            //ABSOLUTE
            if (path.Length > 0 && path[0] == System.IO.Path.DirectorySeparatorChar
                || path.Length > 1 && path[1] == System.IO.Path.VolumeSeparatorChar)
                return new BitmapImage(new Uri(path));

            //RELATIVE
            return new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + path));
        }
        catch (Exception)
        {
            return new BitmapImage();
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML side:

<UserControl.Resources>
    <local:ImageConverter x:Key="MyImageConverter" />
    (...)
</UserControl.Resources>

<Image Source="{Binding Products/Image, Converter={StaticResource MyImageConverter}}">

Cheers,

Sérgio

Comments

3

Environment.CurrentDirectory will show you the folder the .exe is stored in (that is unless you manually set a .CurrentDirectory - but then we can assume you already know where it is).

Comments

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.