2016-03-03 35 views
-1

下面的此方法將圖像從圖像文件路徑轉換爲ImageSource。 Solution found hereWPF圖像控件不顯示以編程方式添加/分配的ImageSource

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

以下是我測試過的路徑看起來像enter image description here

這是ImageSource的被分配方式:

Image_Control.Source = GetImageSourceFromPath(path); 

的問題是圖像不顯示WPF圖像控件。

任何幫助將不勝感激。謝謝。

+0

你應該改變'UriKind.Relative'到'UriKind.RelativeOrAbsolute'。它允許您使用絕對或相對路徑設置圖像。 – bars222

回答

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

謝謝!你剛剛救了我幾個小時的搜索!!!!! – HaloMediaz

1

您的代碼將正常工作,並進行以下更改。

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

    Image_Control.Source = GetImageSourceFromPath(path); 

如果您使用的完整路徑無需指定UriKind,其他明智使用UriKind.RelativeOrAbsolute

+0

非常感謝您花時間回答我的問題。 – HaloMediaz

相關問題