UWP 中的 BitmapImage 到 StorageFile

BitmapImage to StorageFile in UWP

我通过这样做得到了一个 BitmapImage:

var bitmap = new BitmapImage(new Uri(url, UriKind.Absolute))

但是,因为我正在使用 Cortana 应用程序,所以我需要使用 StorageFile 作为图像类型(我觉得这很愚蠢,但事实就是如此)

那么,如何将该位图转换为我需要的 StorageFile,或者如何通过 URL 下载图像到 StorageFile,因为

StorageFle.GetFileFromApplicationUriAsync(Uri uri)

不断弹出 "Value does not fall within the specified range"

的异常

非常感谢任何帮助,谢谢!

StorageFle.GetFileFromApplicationUriAsync用于获取app资源

要将远程 URI 下载为 StorageFile,请使用 StorageFile.CreateStreamedFileFromUriAsync

var path = "https://cdn.sstatic.net/Sites/Whosebug/company/img/logos/so/so-logo.png";
var file = await StorageFile.CreateStreamedFileFromUriAsync(Path.GetFileName(path), 
    new Uri(path), null);

您也可以将文件保存到磁盘检查是否正确下载。

await file.CopyAsync(ApplicationData.Current.LocalFolder);