Wpf - 相对图像源路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1444699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:01:01  来源:igfitidea点击:

Wpf - relative image source path

wpfimagepath

提问by stiank81

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:

我在 Wpf 应用程序中设置图像源时遇到问题。我有一个图像,其中源绑定到 DataContext 对象的 SourceUri 属性,如下所示:

<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.

现在,我不知道在我的对象的 SourceUri 属性上设置什么。设置完整的绝对路径(“c:/etc/image.jpg”)它显示得很好,但显然我想设置相对路径。我的图像存储在文件夹 Resources 中,该文件夹与我的应用程序文件夹位于同一文件夹中。最后,这些图像可能来自任何地方,因此将它们添加到项目中确实不是一种选择。

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.

我已经尝试了相对于应用程序文件夹的路径,以及相对于工作路径(调试文件夹)的路径。还尝试使用“pack://..”语法,但没有运气,但读到这没有任何意义。

Any hints on what I should try?

关于我应该尝试什么的任何提示?

采纳答案by Mark Heath

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:

也许您可以使 DataContext 对象的 SourceUri 属性更聪明一点,并确定应用程序文件夹是什么,并基于此返回绝对路径。例如:

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

回答by Holf

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

System.IO.Path 中有一个方便的方法可以帮助解决这个问题:

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.

这应该返回 'C:\Folders\MoreFolders\Resources\image.jpg' 或您上下文中的任何完整路径。它将使用当前工作文件夹作为起点。

Link to MSDN documentation on GetFullPath.

链接到 GetFullPath 上的 MSDN 文档。

回答by sergiol

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:

C#方面:

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:

XAML 端:

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

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

Cheers,

干杯,

Sérgio

塞尔吉奥

回答by Nick Udell

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).

Environment.CurrentDirectory 将向您显示 .exe 存储的文件夹(除非您手动设置 .CurrentDirectory - 但我们可以假设您已经知道它在哪里)。