2011-05-03 38 views
7

tôi đã cố gắng sử dụng mã này cho hình ảnh tải về:Lưu hình ảnh từ xa để lưu trữ Isolated

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

Vấn đề là: làm thế nào có được dòng hình ảnh?

Cảm ơn!

Trả lời

0

Hãy thử như sau

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

Sau đó, bạn có thể sử dụng sau

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

'System.Drawing.Image' không có sẵn trong Silverlight –

5

Thật dễ dàng để có được một dòng vào một tập tin trong Isolated Storage. IsolatedStorageFile có phương thức OpenFile nhận một phương thức.

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

Bạn cần phải đặt e.Result như một tham số khi gọi PicToIsoStore bên trong phương pháp client_DownloadStringCompleted bạn

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

Lớp WebClient được đáp ứng và lưu trữ nó trong biến e.Result. Nếu bạn xem xét cẩn thận, loại e.Result đã là Stream nên nó đã sẵn sàng để được chuyển tới phương pháp của bạn PicToIsoStore

2

Có một cách dễ dàng

WebClient client = new WebClient(); 
client.OpenReadCompleted += (s, e) => 
{ 
    PicToIsoStore(e.Result); 
}; 
client.OpenReadAsync(new Uri("http://mysite/image.png", UriKind.Absolute)); 
Các vấn đề liên quan