简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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