2011-05-03 16 views
7

He intentado utilizar el código de la imagen descarga para:Guardar una imagen remota para el almacenamiento aislado

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

El problema es: ¿cómo obtener la corriente de imagen?

¡Gracias!

Respuesta

0

intente lo siguiente

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

continuación, puede utilizar el siguiente

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

'System.Drawing.Image' no está disponible en Silverlight –

5

Es fácil conseguir un flujo a un archivo en el almacenamiento aislado. IsolatedStorageFile tiene un método OpenFile que obtiene uno.

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

Es necesario poner e.Result como parámetro al llamar PicToIsoStore dentro de su client_DownloadStringCompleted método

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

La clase WebClient obtiene respuesta y lo almacena en la variable e.Result. Si se fijan bien, el tipo de e.Result ya está Stream por lo que está listo para ser pasado a su método de PicToIsoStore

2

Hay una manera fácil

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