【问题标题】:How to download and store an image using Windows.Web.Http?如何使用 Windows.Web.Http 下载和存储图像?
【发布时间】:2014-11-02 22:15:49
【问题描述】:

如何使用 Windows.Web.Http 从 Internet 下载 jpeg 图像并将其存储在 Windows Store 应用程序中?

我面临的问题是我不知道我必须对图像使用什么 Get...Async 和 Write...Async 方法?文件与字符串有很大不同。


仅限 Windows.Web.Http

没有第三方解决方案

如果您提出其他建议,请使用评论部分,而不是答案。谢谢!


…    
using Windows.Storage;
using Windows.Web.Http;

Uri uri = new Uri("http://image.tmdb.org/t/p/w300/" + posterPath);
HttpClient httpClient = new HttpClient();

// I guess I need to use one of the Get...Async methods?
var image = await httpClient.Get…Async(uri);

StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder cachedPostersFolder = await localFolder.CreateFolderAsync("cached posters", CreationCollisionOption.OpenIfExists);

StorageFile posterFile = await cachedPostersFolder.CreateFileAsync(posterPath, CreationCollisionOption.ReplaceExisting);

// I guess I need to use one of the Write...Async methods?
await FileIO.Write…Async(posterFile, image);

【问题讨论】:

标签: c# windows-phone-8 windows-8 windows-store-apps windows-phone-8.1


【解决方案1】:

您可以使用GetBufferAsync 方法获取缓冲区,然后调用FileIO.WriteBufferAsync 将缓冲区写入文件:

Uri uri = new Uri("http://i.stack.imgur.com/ZfLdV.png?s=128&g=1");
string fileName = "daniel2.png";

StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
      fileName, CreationCollisionOption.GenerateUniqueName);


HttpClient client = new HttpClient();

var buffer = await client.GetBufferAsync(uri);
await Windows.Storage.FileIO.WriteBufferAsync(destinationFile, buffer);

【讨论】:

    【解决方案2】:
     image1.Source = new BitmapImage(new Uri("http://www.image.com/image.jpg",     UriKind.RelativeOrAbsolute));
    
    
           using (var mediaLibrary = new MediaLibrary())
            {
                using (var stream = new MemoryStream())
                {
                    var fileName = string.Format("Gs{0}.jpg", Guid.NewGuid());
                    bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
                    stream.Seek(0, SeekOrigin.Begin);
                    var picture = mediaLibrary.SavePicture(fileName, stream);
                    if (picture.Name.Contains(fileName)) return true;
                }
            }
    

    【讨论】:

      【解决方案3】:

      这与 John 的答案类似,但是在 WP8.1 中您不能使用 GetBufferAsync。相反,您可以按照我的方式使用 GetStreamAsync:

      Uri uri = new Uri(UriString);
      string fileName = p4.IconLocation;
      
      HttpClient client = new HttpClient();
      
      var streamImage = await client.GetStreamAsync(uri);
      
      await SaveToLocalFolderAsync(streamImage, fileName);
      

      使用函数:

      public async Task SaveToLocalFolderAsync(Stream file, string fileName)
          {
              StorageFolder localFolder = ApplicationData.Current.LocalFolder;
              StorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
              using (Stream outputStream = await storageFile.OpenStreamForWriteAsync())
              {
                  await file.CopyToAsync(outputStream);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2015-01-13
        • 2016-11-19
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 2018-01-09
        • 2018-03-19
        • 2020-11-06
        相关资源
        最近更新 更多