UWP 将 BitmapImage 转换为 WriteableBitmap
UWP Converting BitmapImage to WriteableBitmap
最终我的目标是将从远程服务器通过 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 image UIelement?
中显示
3. 提供 DownloadProgress
和 ImageOpened
或 downloaded
?
的回调
我简直不敢相信这是多么棘手。
WritableBitmap中的"writable"指的是可编辑(不是指可写入磁盘)
为了将下载的图像文件写入磁盘,您不需要 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;
最终我的目标是将从远程服务器通过 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 image UIelement?
中显示
3. 提供 DownloadProgress
和 ImageOpened
或 downloaded
?
我简直不敢相信这是多么棘手。
WritableBitmap中的"writable"指的是可编辑(不是指可写入磁盘)
为了将下载的图像文件写入磁盘,您不需要 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;