Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

Wpf - relative image source path

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

This should return 'C:FoldersMoreFoldersResourcesimage.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...