2011-10-10 11 views
5

Do thiếu kinh nghiệm lập trình (3 tháng), tôi không thể tạo lại bất kỳ ví dụ nào về câu hỏi trên. Các ví dụ tôi đã tìm thấy liên quan đến Silverlight không WP7, tiết kiệm hình ảnh dựa trên máy ảnh, đã được phức tạp cho nhu cầu của tôi hoặc có chỉ không làm việc. Tôi đã có thể tải về một tập tin văn bản bằng cách sử dụng một thể hiện của Webclient và lưu nó vào bộ nhớ bị cô lập bằng cách sử dụng StreamWriter. Tôi cần phải hoàn thành nhiệm vụ tương tự với hình ảnh jpg. Dưới đây là những gì tôi đã sử dụng để tải xuống tệp văn bản.Làm cách nào để tải xuống hình ảnh (jpg) qua webclient và lưu vào bộ nhớ bị cô lập trên Windows Phone 7?

============================================== =================================

IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication(); 


    private void GetTextFile() 
     { 
      WebClient client = new WebClient(); 
      client.DownloadStringCompleted += new  DownloadStringCompletedEventHandler(client_DownloadStringCompleted); 
      client.DownloadStringAsync(new Uri("http://mywebsite.com/textfile.txt")); 
     } 

    private void client_DownloadStringCompleted(object sender,  DownloadStringCompletedEventArgs e) 
     { 
      StreamWriter MyStreamWriter = new StreamWriter(new  IsolatedStorageFileStream("textfile.txt", FileMode.Create, MyStore)); 
      MyStreamWriter.WriteLine(e.result) 
      MyStreamWriter.Close(); 
    } 

========== ================================================== ===================

Tôi đã xóa một vài dòng được sử dụng để xử lý các lỗi v.v. để giữ cho nó đơn giản như có thể thực hiện được.

Xin vui lòng ai đó có thể sửa đổi ở trên để cho phép tôi tải xuống và lưu jpg?

Hãy giữ nó đơn giản nhất có thể vì tôi dễ nhầm lẫn.

Cảm ơn bạn đã dành thời gian trước!

RESOLVED! ============================================ ===================================

Tôi quản lý để làm cho nó hoạt động bằng cách sử dụng thông tin từ trang web này phía dưới. http://dotnet.dzone.com/articles/operating-image-files-windows

Hy vọng điều này sẽ giúp những người mới thất vọng khác trong tương lai!

IsolatedStorageFile MyStore = IsolatedStorageFile.GetUserStoreForApplication(); 

private void GetImageFile() 
{ 
     WebClient client = new WebClient(); 
     client.OpenReadCompleted += new  OpenReadCompletedEventHandler(client_OpenReadCompleted); 
     client.OpenReadAsync(new Uri("http://mywebsite.com/1.jpg"), client); 
    } 


void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 
    var resInfo = new StreamResourceInfo(e.Result, null); 
var reader = new StreamReader(resInfo.Stream); 
byte[] contents; 
    using (BinaryReader bReader = new BinaryReader(reader.BaseStream)) 
    { 
    contents = bReader.ReadBytes((int)reader.BaseStream.Length); 
    } 
    IsolatedStorageFileStream stream = MyStore.CreateFile("10.jpg"); 
stream.Write(contents, 0, contents.Length); 
stream.Close(); 
} 

Trả lời

4

Hãy thử thế này, lẽ hữu ích cho bạn,

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    HttpWebRequest reqest1 = (HttpWebRequest)WebRequest.Create(url); 
    reqest1.BeginGetResponse(DownloadImageCallback, reqest1); 
    Thread.Sleep(1000); 
} 

void DownloadImageCallback(IAsyncResult result) 
{ 

    HttpWebRequest req1 = (HttpWebRequest)result.AsyncState; 
    HttpWebResponse responce = (HttpWebResponse)req1.EndGetResponse(result); 
    Stream s = responce.GetResponseStream(); 
    Deployment.Current.Dispatcher.BeginInvoke(() => 
    { 
     string directory = "Images"; 
     if (!myStore.DirectoryExists(directory)) 
     { 
      myStore.CreateDirectory(directory); 
      using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//yourfilename.jpg")) 
       { 
        var wb = new WriteableBitmap(bitimage); 
        var width = wb.PixelWidth; 
        var height = wb.PixelHeight; 
        Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100); 
       } 
      } 
     } 
     else 
     { 
      using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       if (myIsolatedStorage.FileExists(directory + "//yourfilename.jpg")) 
       { 
        myIsolatedStorage.DeleteFile(directory + "//yourfilename.jpg"); 
       } 

       using (var isoFileStream = myIsolatedStorage.CreateFile(directory + "//yourfilename.jpg")) 
       { 
        var wb = new WriteableBitmap(bitimage); 
        var width = wb.PixelWidth; 
        var height = wb.PixelHeight; 
        Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100); 
       } 
      } 
     } 
    }); 
}