使用HttpClient下载的图像已损坏

时间:2012-08-10 10:26:25

标签: c# windows-runtime

我正在尝试将图像从Web保存到本地存储以便稍后进行操作,但它似乎已损坏并且尝试使用外部应用程序打开它失败。在webbrowser中打开图像完全正常。谢谢你的帮助。

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);
}

1 个答案:

答案 0 :(得分:1)

您必须致电StoreAsync

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