UWP - ImageBrush full path to ImageSource

非 Y 不嫁゛ 提交于 2019-12-13 19:45:23

问题


I have an ellipse and I want to fill it with image. And I would like to set full path to the image to ImageSource property of ImageBrush. But I wasn't able to accomplish it.

I've tried to add:

  • C:/Users/someuser/Pictures/untitled.png

  • C:\\Users\\someuser\\Pictures\\untitled.png which came from FilePicker

  • C:\Users\misko\Pictures\untitled.png.

But non of this works. Can you please explain to me how to properly set full path?


回答1:


You have to open the file first using FilePicker

if (file != null)
{
    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

    var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
    await bitmapImage.SetSourceAsync(stream);

    var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

    grid.Background = new ImageBrush
    {

        ImageSource = bitmapImage
    };
}


来源:https://stackoverflow.com/questions/36736728/uwp-imagebrush-full-path-to-imagesource

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