How to download and store an image using Windows.Web.Http?

被刻印的时光 ゝ 提交于 2019-12-07 12:14:47

问题


How do I download and store a jpeg image from the internet in a Windows Store App with Windows.Web.Http?

The problem that I am facing is that I don't know what Get…Async and Write…Async method I must use for an image? It is very different with files, than with strings.


Only Windows.Web.Http!

No third-party solutions!

If you suggest something else, please use the comment section, not the answer. Thank you!


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

回答1:


You can get a buffer using the GetBufferAsync method and then call the FileIO.WriteBufferAsync to write the buffer to a file:

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:


This is a similar answer to John's, however in WP8.1 you can't use GetBufferAsync. Instead you can use GetStreamAsync in the way that I have:

Uri uri = new Uri(UriString);
string fileName = p4.IconLocation;

HttpClient client = new HttpClient();

var streamImage = await client.GetStreamAsync(uri);

await SaveToLocalFolderAsync(streamImage, fileName);

using the function:

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


来源:https://stackoverflow.com/questions/26705076/how-to-download-and-store-an-image-using-windows-web-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!