且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从网络下载和保存图像?

更新时间:2023-12-05 15:57:52

在手机上,您可以使用HttbWebRequest建议避免UI影响)或WebClient,我发布在这里的项目。



WebClient,HttpWebRequest和Windows Phone 7上的UI线程



然后,您可以将流传送到这种形式的东西,将其写入隔离的存储空间。

  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是正确的,你也可以使用MediaLibrary.SavePicture。请注意,这将使图片与图片集中的用户照片混合在一起。



这与
$ b一样简单$ b

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

将其存储在隔离存储中基本上是您的应用程序专用文件系统。 >

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?

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.