-1

This method below converts an image from an image file-path to an ImageSource. Solution found here

 public static ImageSource GetImageSourceFromPath(string path)
 {
     return new BitmapImage(new Uri(path, UriKind.Relative));
 }

Here is what a path I've tested looks likeenter image description here

This is how the imagesource gets assigned:

Image_Control.Source = GetImageSourceFromPath(path);

The problem is that the image doesn't show in the WPF image control.

Any help would be much appreciated. Thanks.

Community
  • 1
  • 1
HaloMediaz
  • 1,251
  • 2
  • 13
  • 31
  • You should change `UriKind.Relative` to `UriKind.RelativeOrAbsolute`. It allows you to set images with absolute or relative paths. – bars222 Mar 03 '16 at 05:15

2 Answers2

1

Your code will works fine with the following changes.

 public static ImageSource GetImageSourceFromPath(string path)
     {
         return new BitmapImage(new Uri(path));
     }

    Image_Control.Source = GetImageSourceFromPath(path);

If you are using full path no need to specify the UriKind,other wise use UriKind.RelativeOrAbsolute

VVN
  • 1,607
  • 2
  • 16
  • 25
1
public static ImageSource GetImageSourceFromPath(string path)
 {
     return new BitmapImage(new Uri(path, **UriKind.RelativeOrAbsolute**));
 }
zhaojingbo
  • 131
  • 6