Save the remote image in isolated storage

I tried using this code to upload an image:

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); } } } 

The problem is this: how to get the image stream?

Thanks!

+7
source share
4 answers

It is easy to get a stream to a file in isolated storage. IsolatedStorageFile has an OpenFile method that gets one.

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

You need to put e.Result as a parameter when calling PicToIsoStore inside your client_DownloadStringCompleted method

 void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { PicToIsoStore(e.Result); } 

The WebClient class receives the response and stores it in the e.Result variable. If you look closely, the e.Result type is already Stream , so it is ready to be passed to your PicToIsoStore method

+5
source

There is an easy way.

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

Try to execute

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

Then you can use the following

 var stream = myImage.ToStream(ImageFormat.Gif); 
0
source

All Articles