简体   繁体   English

如何在 UWP C# 中显示由文件打开选择器选择的图像?

[英]How to display an image chosen by a file open picker in UWP C#?

I implemented a File Open Picker into a Universal Windows Platform Application for choosing an image to display in a list item.我在通用 Windows 平台应用程序中实现了一个文件打开选择器,用于选择要在列表项中显示的图像。

However, after getting the image path from the file open picker, the path can't be set as an image source neither as a URI nor network path (tried "file:///" + path).但是,从文件打开选择器中获取图像路径后,无法将路径设置为图像源,既不能作为 URI,也不能作为网络路径(尝试“file:///”+路径)。

Is there a way to show an image from a file open picker selected path?有没有办法从文件打开选择器选择的路径中显示图像?

If not, is there any way to open a local image from the computer in the app?如果没有,有没有办法在应用程序中从计算机打开本地图像?

I implemented a File Open Picker into a Universal Windows Platform Application for choosing an image to display in a list item.我在通用 Windows 平台应用程序中实现了一个文件打开选择器,用于选择要在列表项中显示的图像。

UWP does not support file:// uri scheme, if you open the file with file open picker, you could open the file as stream, and convert it to BitmapImage like the following. UWP 不支持file:// uri 方案,如果您使用文件打开选择器打开文件,您可以将文件打开为 stream,并将其转换为BitmapImage如下所示。

try
{
    var picker = new Windows.Storage.Pickers.FileOpenPicker
    {
        ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
        SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
    };
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".png");

    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
    if (file != null)
    {
        using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            BitmapImage bitmapImage = new BitmapImage();
            await bitmapImage.SetSourceAsync(fileStream);
            MyImage.Source = bitmapImage;

        }
    }
   
}
catch (Exception ex)
{

    throw ex;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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