2011-10-10 50 views
5

由於缺乏編程經驗(3個月),我一直無法重新創建任何找到的上述問題的例子。我發現的例子涉及到非WP7 Silverlight,基於相機的圖像保存,已經根據我的需要複雜化或者沒有工作。我可以使用Webclient實例下載文本文件,並使用StreamWriter將其保存到獨立存儲中。我需要用JPG圖像來完成相同的任務。以下是我用來下載文本文件的內容。如何通過Web客戶端下載圖像(jpg)並保存到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(); 
    } 

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

我已刪除了用來處理錯誤等,以保持它的簡單,更多鈔票的幾行。

請會有人修改上面,使我能夠下載並保存JPG?

請保持儘可能簡單,因爲我很容易混淆。

謝謝您提前預約!

解決!============================================ ===================================

我設法得到它的工作使用本網站的信息下面。 http://dotnet.dzone.com/articles/operating-image-files-windows

希望這將有助於其他未來的沮喪的新手!

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

回答

4

試試這個,也許有幫助到您,

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); 
       } 
      } 
     } 
    }); 
} 
相關問題