Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将远程映像保存到独立存储中_C#_Silverlight_Windows Phone 7 - Fatal编程技术网

C# 将远程映像保存到独立存储中

C# 将远程映像保存到独立存储中,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,我尝试使用以下代码下载图像: void downloadImage(){ WebClient client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); client.DownloadStringAsync(new Uri("http://mysite/im

我尝试使用以下代码下载图像:

void downloadImage(){
 WebClient client = new WebClient();
 client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.DownloadStringAsync(new Uri("http://mysite/image.png"));

        }

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
           //how get stream of image?? 
           PicToIsoStore(stream)
        }

        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);
                }
            }
        }
问题是:如何获得图像流

谢谢!

试试下面的方法

public static Stream ToStream(this Image image, ImageFormat formaw) {
  var stream = new System.IO.MemoryStream();
  image.Save(stream);
  stream.Position = 0;
  return stream;
}
var stream = myImage.ToStream(ImageFormat.Gif);
然后您可以使用以下命令

public static Stream ToStream(this Image image, ImageFormat formaw) {
  var stream = new System.IO.MemoryStream();
  image.Save(stream);
  stream.Position = 0;
  return stream;
}
var stream = myImage.ToStream(ImageFormat.Gif);

在隔离存储中很容易将流获取到文件中
IsolatedStorageFile
有一个方法可以获取一个

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream stream = store.OpenFile("somepic.jpg", FileMode.Open))
    {
        // do something with the stream
    }
}

调用
PictoIsToStore
时,需要将
e.Result
作为参数放入
client\u DownloadStringCompleted
方法中

void client_DownloadStringCompleted(object sender,
     DownloadStringCompletedEventArgs e)
        {
           PicToIsoStore(e.Result);
        }
WebClient类获取响应并将其存储在
e.Result
变量中。如果仔细观察,e.Result的类型已经是
,因此可以将其传递给您的方法
PictoIstore

有一种简单的方法

WebClient client = new WebClient();
client.OpenReadCompleted += (s, e) =>
{
    PicToIsoStore(e.Result);
};
client.OpenReadAsync(new Uri("http://mysite/image.png", UriKind.Absolute));

System.Drawing.Image
在Silverlight中不可用