通过Windows 8 Metro XAML App下载并保存图片库中的图片 [英] Download and Save image in Pictures Library through Windows 8 Metro XAML App

查看:188
本文介绍了通过Windows 8 Metro XAML App下载并保存图片库中的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个简单的Windows 8 Metro应用程序,它只需从给定的URL下载图像文件(例如 http ://sample.com/foo.jpg ),然后将其保存到图片库。

I am trying to develop a simple Windows 8 Metro app which simply downloads an image file from a given URL (say http://sample.com/foo.jpg) and then save it to Pictures Library.

我在UI中有一个图像控件来显示下载的图片。
我也很难将图像控件的图像源设置为新下载的图像(实际上我甚至不能下载它)。

I have an image control in the UI to display the downloaded image. I'm also facing difficulty in setting the image source for the image control to the newly downloaded image (actually I'm not even able to download it).

另外,是否可以将图像文件存储在图片库中的特定文件夹中(如果不存在,那么应用程序应该创建它)?

Also, is it possible to store the image file in a particular folder in the Pictures library (if it doesn't exist, then the app should create it)?

我真的困在这里。

请帮助我。

推荐答案

p>这里有一些粗略的代码,我相信完成你想要的。它假设您有两个图像控件(Image1和Image2),并且您在清单中选中了图片库功能。请查看 XAML图像示例以及

Here's some rough code that I believe accomplishes what you want. It assumes you have two image controls (Image1 and Image2) and that you have the Pictures Library capability checked in the manifest. Take a look at the XAML images sample as well

        Uri uri = new Uri("http://www.picsimages.net/photo/lebron-james/lebron-james_1312647633.jpg");
        var fileName = Guid.NewGuid().ToString() + ".jpg";

        // download pic
        var bitmapImage = new BitmapImage();
        var httpClient = new HttpClient();
        var httpResponse = await httpClient.GetAsync(uri);
        byte[] b = await httpResponse.Content.ReadAsByteArrayAsync();

        // create a new in memory stream and datawriter
        using (var stream = new InMemoryRandomAccessStream())
        {
            using (DataWriter dw = new DataWriter(stream))
            {
                // write the raw bytes and store
                dw.WriteBytes(b);
                await dw.StoreAsync();

                // set the image source
                stream.Seek(0);
                bitmapImage.SetSource(stream);

                // set image in first control
                Image1.Source = bitmapImage;

                // write to pictures library
                var storageFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
                    fileName, 
                    CreationCollisionOption.ReplaceExisting);

                using (var storageStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await RandomAccessStream.CopyAndCloseAsync(stream.GetInputStreamAt(0), storageStream.GetOutputStreamAt(0));
                }
            }
        }

        // read from pictures library
        var pictureFile = await KnownFolders.PicturesLibrary.GetFileAsync(fileName);
        using ( var pictureStream = await pictureFile.OpenAsync(FileAccessMode.Read) )
        {
            bitmapImage.SetSource(pictureStream);
        }
        Image2.Source = bitmapImage;
    }

这篇关于通过Windows 8 Metro XAML App下载并保存图片库中的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆