2

In my WPF I want images on the screen to change, every time when the user clicks right button. The problem is that I have all the time same error message:

'Invalid URI: The format of the URI could not be determined.'

This is the code:

string pic1 = @"C:/Users/Milk/Desktop/exercises/wpf_1/portraits/1.png";
string pic2 = @"C:/Users/Milk/Desktop/exercises/wpf_1/portraits/2.png";

private void buttonRight_Click(object sender, RoutedEventArgs e)
{
     List<string> portraits = new List<string>();
     portraits.Add(pic1);
     portraits.Add(pic2);
     string ShowPicture = portraits[counter % portraits.Count];
     image.Source = new BitmapImage(new Uri(portraits.ToString()));
     counter++;
}

When I tried just with one string, like this:

image.Source = new BitmapImage(new Uri(pic1));

it was working fine, but once it's in the list, it cannot find the file path - at least, that's how this looks like for me.

Any idea how to fix this and where I am making an error?

TrevorBrooks
  • 3,590
  • 3
  • 31
  • 53
milk
  • 23
  • 2

2 Answers2

1

It is because .ToString() usually returns namespace of an object (unless overriden), which in this case is List's namespace; you need to pass in actual list values one by one into Uri constructor.

What you need to do is to pass in actual path as such:

string ShowPicture = portraits[counter % portraits.Count];
image.Source = new BitmapImage(new Uri(ShowPicture));
Karolis Kajenas
  • 1,523
  • 1
  • 15
  • 23
  • ShowPicture doesn't have the path of the picture he wants to display but your answer is right , he is biding it to the object list , as you said using toString() will return something like A929D9933929919 --> the object reference – napi15 Sep 07 '17 at 18:58
0

Hi you are linking it to the list Object and not the element inside the list

This should solve your issue :

image.Source = new BitmapImage(new Uri(portraits[0].ToString()));

that will get the pic1

and if you want to get the pic2

you will need to write :

image.Source = new BitmapImage(new Uri(portraits[1].ToString()));

if you want to get both of pic you need to add a loop

Something like :

for (int i=0 ; i < portraits.count ; i++) 
image.Source = new BitmapImage(new Uri(portraits[i].ToString()));
//..Do the rest 

portraits , let me know exactly your expected result and I will add further details

napi15
  • 2,354
  • 2
  • 31
  • 55