简体   繁体   English

使用HttpClient下载的图像已损坏

[英]Image downloaded with HttpClient is corrupt

I am trying to save an image from the web to local storage to be manipulated later, but it appears to be corrupt and attempting to open it with an external application fails. 我正在尝试将图像从Web保存到本地存储中,以备后用,但它似乎已损坏,尝试使用外部应用程序打开它失败。 Opening the image in the webbrowser works completely normally. 在网络浏览器中打开图像完全正常。 Thanks for any help. 谢谢你的帮助。

var client = new HttpClient();
var clientResponse = await client.GetByteArrayAsync(imageUri);

var temp = ApplicationData.Current.TemporaryFolder;
StorageFile file;
if ((await temp.GetFilesAsync()).Any(f => f.Name == "temp_image.png")) {
    file = await temp.GetFileAsync("tempcolorizer.png");
} else {
    file = await temp.CreateFileAsync("temp_image.png");
}
using (var fs = await file.OpenReadAsync())
using (var writer = new DataWriter(fs)) {
    writer.WriteBytes(clientResponse);
}

You have to call StoreAsync : 您必须致电StoreAsync

using (var fs = await file.OpenReadAsync())
using (var writer = new DataWriter(fs)) {
  writer.WriteBytes(clientResponse);
  await writer.StoreAsync();
}

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

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