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中不可用