Usher
Usher

Reputation: 2146

how to add images from file location WPF

Am stuck to load images from my file location in WPF.

here is my xaml

<Image Grid.ColumnSpan="3" Grid.Row="11" Height="14" HorizontalAlignment="Left" Margin="57,1,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="108" />

Here is my code behind

internal int FindImages(string slugName, DirectoryInfo outputFolder)
{
    if (slugName != null)
    {
        List<string> filePathList = Directory.GetFiles(outputFolder.FullName).ToList();
        List<string> filePathList_ToBeDeleted = new List<string>();
        foreach (string filePath in filePathList)
        {                  
            if (Path.GetFileNameWithoutExtension(filePath).ToLower().Contains("_70x70"))
            {                           
                image1.Source = filePath;
            }                   
        }
        int count = 0;

        return count;
    }

My file path shows like "\\\\Server1\\Dev\\Online\\Images\\7PMa_Test3_0306_70x70.jpg"

Upvotes: 14

Views: 55985

Answers (2)

user1780040
user1780040

Reputation: 81

If it's a file located somewhere on the drive (not a resource), better use an ABSOLUTE path:

image.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "image.png", UriKind.Absolute));

This code detects the running folder and builds the path relative to it

Upvotes: 6

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

Here's the catch

image1.Source = new BitmapImage(new Uri(filePath));

Upvotes: 45

Related Questions