AliSafari186 AliSafari186 - 1 month ago 22
C# Question

how to change image source to address on disk in runtime wpf c#

This link answers how to change image source in run-time but it's not work with addresses from disk like below:

recivedPic.Source = new BitmapImage(new Uri(@"C:\Users\ali\Desktop\Rubik-SMS\Rubik-SMS\Rubik-SMS\Images\Back.jpg" , UriKind.Relative));


Only work with addresses like below(pictures belong to project):

recivedPic.Source = new BitmapImage(new Uri(@"Images\Back.jpg" , UriKind.Relative));


I need the first one but nothing shows, any suggestion?

mww mww
Answer

You need use BitmapImage with uri only. Absolute path isn't a relative so you cant use UriKind.Relative.

Absolute URIs are characterized by a complete reference to the resource (example: http://www.contoso.com/index.html), while a relative Uri depends on a previously defined base URI (example: /index.html).

this.yourImage.Source = new BitmapImage(new Uri("c:\\img.jpg"));

or

img.BeginInit();
img.UriSource = new Uri("d:\\img.jpg");
img.EndInit();
this.image1.Source = img;