How can I download and save images from the web?

2019-01-24 15:12发布

I'm trying to make a Windows Phone 7 app that will save some images off the web, I have no idea where I can or if I can save images from the web to the phone.

What can I do to save images?

2条回答
叼着烟拽天下
2楼-- · 2019-01-24 16:11

On the phone, you can use HttbWebRequest (recommended to avoid UI impact) or WebClient per the project I posted here.

WebClient, HttpWebRequest and the UI Thread on Windows Phone 7

You can then take your stream and pass it into something of this form to write it to isolated storage.

private void PicToIsoStore(Stream pic) {
    using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
        var bi = new BitmapImage();
        bi.SetSource(pic);
        var wb = new WriteableBitmap(bi);
        using (var isoFileStream = isoStore.CreateFile("somepic.jpg")) {
            var width = wb.PixelWidth;
            var height = wb.PixelHeight;
            Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
        }
    }
}

Jon is correct you can also use MediaLibrary.SavePicture. Be aware that this would put the pics mixed in with the users photos in the Picture Hub.

This is as straight forward as

private void PicToMediaLibary(Stream pic) {
     MediaLibrary lib = new MediaLibrary();
     lib.SavePicture("blah", pic);
}

Storing it in isolated storage is basically your apps private file system.

查看更多
成全新的幸福
3楼-- · 2019-01-24 16:14

You can download them using WebClient or WebRequest to get the raw bytes.

You can then save to isolated storage fairly easily - but I don't believe you can save in any location seen by the Picture hub, if that's what you were aiming for. (I can't see anything in the Microsoft.Phone.Tasks namespace which would be relevant. There are tasks to capture the camera of choose a photo, but not to save one.)

EDIT: Ooh, I've just found a way. You can use the MediaLibrary class and its SavePicture method... although that's in XNA, so I'd at least have concerns about it working from a Silverlight app. I know some bits of the XNA API do work from Silverlight, and some don't. Worth experimenting with.

查看更多
登录 后发表回答