简体   繁体   中英

WPF Image control doesn't display ImageSource that was added/assigned programatically

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 like 在此处输入图片说明

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM