change image source programmatically

自古美人都是妖i 提交于 2019-12-24 18:20:31

问题


I cannot figure out how to dynamically load an image from the disk to a Xaml image control.

<Image x:Name="imgLogo" ....../>

Then in my c++ class, i cannot get any examples working that I have found. Most are in c#. One annoyance is that I keep seeing System.Windows.Media::ImageSource used, but that is not in my framework.

Can someone please show me an example. I'm coming from the iOS world and am not used to MS and their frameworks.

Thanks,


回答1:


Try this, it's in C# but should be fairly easy to convert:

using Windows.UI.Xaml.Media.Imaging;

<Image Margin="5" Source="{Binding BMImage}" Height="100"/>


bmImage = new BitmapImage();
bmImage.UriSource = new Uri(new Uri(
     *your file path*, 
     *your image name*);


BitmapImage bmImage;
public BitmapImage BMImage
{
    get
    {
        return bmImage;
    }
}

Taken from here (my blog).

To change the image, just change the value of bmImage and call NotifyPropertyChanged(()=>BMImage); (assuming you have that setup)

See here for Microsoft's example of how to use images.

See here for the Windows.UI.Xaml.Media.Imaging namespace.



来源:https://stackoverflow.com/questions/12323864/change-image-source-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!