Xaml UWP将位图图像转换为可写位图

Xaml UWP将位图图像转换为可写位图,xaml,uwp,c++-cx,Xaml,Uwp,C++ Cx,最终,我的目标是将通过http从远程服务器获取的映像保存到本地存储 我是这样读的 BitmapImage^ im = ref new BitmapImage(); im->CreateOptions = BitmapCreateOptions::IgnoreImageCache; im->DownloadProgress += ref new DownloadProgressEventHandler(this, &Capture::ShowDownloadProgress);

最终,我的目标是将通过http从远程服务器获取的映像保存到本地存储

我是这样读的

BitmapImage^ im = ref new BitmapImage();
im->CreateOptions = BitmapCreateOptions::IgnoreImageCache;
im->DownloadProgress += ref new DownloadProgressEventHandler(this, &Capture::ShowDownloadProgress);
im->ImageOpened += ref new RoutedEventHandler(this, &Capture::ImageDownloaded);
im->UriSource = ref new Uri(URL);
当触发
ImageDownloaded
时,我希望能够将图像保存为.jpg文件。我已具有对目标文件夹的写入权限

我发现了一些方法,可以将图像读入
WriteableBitmap
,但是构造函数需要宽度和高度。。。但我不知道这之前得到的图像

我可以用什么方法来……
1.获取有用格式的图像数据,以便将其写入磁盘?
2.在Xaml图像元素中显示它?
3.是否为
下载进度
图像打开
下载
提供回调

我不敢相信这有多棘手。

可写位图中的“可写”指的是它是可编辑的(不是指可写到磁盘)

要将下载的图像文件写入磁盘,您不需要BitmapImage或WritableBitmap,只需下载流并将其直接写入磁盘即可。然后还可以从同一流创建BitmapImage,以便在XAML图像元素中显示它

// download image and write to disk
Uri uri = new Uri("https://assets.onestore.ms/cdnfiles/external/uhf/long/9a49a7e9d8e881327e81b9eb43dabc01de70a9bb/images/microsoft-gray.png");
StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync("microsoft-gray.png", uri, null);
await file.CopyAsync(ApplicationData.Current.LocalFolder, "microsoft-gray.png", NameCollisionOption.ReplaceExisting);

// create a bitmapimage and display in XAML
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
BitmapImage bitmap = new BitmapImage();
await bitmap.SetSourceAsync(stream);
imageElement.Source = bitmap;

我看不到报告CreateStreamedFileFromUriAsync下载进度的方法。在图像较大且用户连接速度较慢的情况下,显示其仍在工作是一个不错的反馈。您还可以使用HttpClient下载流(并观察进度),然后创建存储文件: