Change image source in code behind - Wpf

后端 未结 5 1898
醉酒成梦
醉酒成梦 2020-12-24 06:43

I need to set image source dynamically, please note my image is in somewhere on the Network, here is my code

BitmapImage logo = new BitmapImage();
logo.Begin         


        
5条回答
  •  鱼传尺愫
    2020-12-24 07:24

    None of the methods worked for me as i needed to pull the image from a folder instead of adding it to the application. The below code worked:

    TestImage.Source = GetImage("/Content/Images/test.png")
    
    private static BitmapImage GetImage(string imageUri)
    {
        var bitmapImage = new BitmapImage();
        
        bitmapImage.BeginInit();
        bitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageUri,             UriKind.RelativeOrAbsolute);
        bitmapImage.EndInit();
        
        return bitmapImage;
    } 
    

提交回复
热议问题