如何从网络下载和保存图像? [英] How can I download and save images from the web?

查看:132
本文介绍了如何从网络下载和保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个Windows Phone 7应用程序,可以将一些图像从网络上保存下来,我不知道我可以在哪里,或者我可以将图像从网络保存到手机。



我可以如何保存图像?

解决方案

在手机上,您可以使用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.

这篇关于如何从网络下载和保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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