Get absolute file path from image in WPF

I have something like this in my xaml:

<Grid>
    <Image Name="image" Source="../../Images/DefaultImage.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Image>
</Grid>

How can I get (using the C # code code for the code below) the absolute path of the image source?

+3
source share
3 answers

When converting from a string or Uri file name, ImageConverter creates a BitmapDecoder and extracts its first frame. Getting Uri should be possible:

((BitmapFrame)image.Source).Decoder.ToString()

, , , Uri, Uri, XAML. Uri XAML, _uri BitmapDecoder, , .NET Framework.

+5

, , -

(YourImage.Source as BitmapImage).UriSource

, AbsolutePath AbsoluteUri UriSource, .

+3

You can try the following:

string path = ((BitmapImage)image.Source).UriSource.AbsolutePath;
+2
source

Source: https://habr.com/ru/post/1740965/


All Articles