Mech0z
Mech0z

Reputation: 3647

Changing wpf image source to a jpg on runtime that is not a resource

I have a personal movieapp that downloads pictures runtime, and I want to display the images instead of a placeholder image I put (If no picture is available)

The code I found that should do the trick changes nothing

I have this WPF image control

<Image Name="MovieImage" Height="Auto" Width="Auto" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="4" Source="NoPhotoAvailable.jpg" />

and then I have

public void MovieList_SelectionChanged(Movie SelectedMovie)
    {
        this.SelectedMovie = SelectedMovie;
        BitmapImage bi = new BitmapImage();

        bi.BeginInit();
        bi.UriSource = new Uri("/155.jpg", UriKind.RelativeOrAbsolute);
        bi.EndInit();
        Console.WriteLine(bi.UriSource.ToString());

        MWV.MovieImage.Source = bi;
    }

Where MWV is where the image control is at

The image is placed in the apps folder and named 155.jpg but nothing is shown at all

I found alot of examples inhere that suggests that this should be pretty close to the solution but apprently not correct

Solution:

        public void MovieList_SelectionChanged(Movie SelectedMovie)
    {
        if (MovieCount == 0)
        {
            Console.WriteLine(MovieCount.ToString());
            MWV.MovieImage.Source = new BitmapImage(new Uri("NoPhotoAvailable.jpg", UriKind.RelativeOrAbsolute));
        }
        else
        {
            this.SelectedMovie = SelectedMovie;
            BitmapImage bi = new BitmapImage();

            bi.BeginInit();
            string path = Path.Combine(Path.GetDirectoryName(typeof(MainWindowController).Assembly.Location), this.SelectedMovie.TMDBID.ToString() + ".jpg");
            bi.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
            bi.EndInit();
            Console.WriteLine(bi.UriSource.ToString());

            MWV.MovieImage.Source = bi;
        }
    }

Upvotes: 0

Views: 4001

Answers (1)

SLaks
SLaks

Reputation: 888223

Remove the / from your path.
A path that starts with a / is an absolute path, so your code looks for a file named 155.jpg in the root of the drive.

Upvotes: 1

Related Questions