2012-06-20 100 views
0

我試圖從URL中獲取圖像並將其保存到獨立存儲中,並從孤立存儲中獲取它並將其顯示在我的WP-7應用程序中。保存和從孤立存儲中檢索圖像時出錯

這裏是初步認識代碼:

public void GetImages() 
{ 
    string uri = "http://sherutnetphpapi.cloudapp.net/mini_logos/" + path; 
    WebClient m_webClient = new WebClient(); 
    imageUri = new Uri(uri); 

    m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_ImageOpenReadCompleted); 
    m_webClient.OpenReadAsync(imageUri); 
} 

void webClient_ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{ 

    Stream stream = e.Result; 
    using (IsolatedStorageFile myIsf = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     IsolatedStorageFileStream fileStream = myIsf.CreateFile(path); 
     StreamResourceInfo sri = null; 
     sri = Application.GetResourceStream(imageUri); 

     BitmapImage bitmap = new BitmapImage(); 
     bitmap.SetSource(sri.Stream); 
     WriteableBitmap wb = new WriteableBitmap(bitmap); 

     // Encode WriteableBitmap object to a JPEG stream. 

     System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
     fileStream.Close(); 
    } 
} 

的問題是在這條線:sri = Application.GetResourceStream(imageUri); 我得到這個方法GetResourceStream()預期相對開放的一個例外,發現絕對的。

我不明白該怎麼給這種方法,而不是imageUri

在此先感謝!

+0

我真的不知道我是什麼新手在這個網站上。我會很高興,如果你可以向我解釋這個比率 – thechmodmaster

+0

雙。我想我明白了.....謝謝 – thechmodmaster

+1

當人們提供有用的答案,他們得票。當答案沒有幫助時,他們會得到反對票。無論您覺得最有幫助或最準確的答案,您都需要接受它作爲「正確答案」。 –

回答

2

我認爲這可能是你需要的。

public void GetImages() 
{ 
    string uri = "http://sherutnetphpapi.cloudapp.net/mini_logos/" + path;  
    WebClient m_webClient = new WebClient(); 
    imageUri = new Uri(uri); 
    m_webClient.OpenReadAsync(imageUri); 
    m_webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_ImageOpenReadCompleted); 
    m_webClient.AllowReadStreamBuffering = true; 

} 


void webClient_ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
{   
    var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication(); 
    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(path, System.IO.FileMode.Create, isolatedfile)) 
    { 
     byte[] buffer = new byte[e.Result.Length]; 
     while (e.Result.Read(buffer, 0, buffer.Length) > 0) 
     { 
      stream.Write(buffer, 0, buffer.Length); 
     } 
    } 
} 
+0

我試過,但我得到一個錯誤:「無法從字符串轉換爲system.uri」 – thechmodmaster

+0

是的,但我不知道什麼文件路徑給它 – thechmodmaster

+0

我想我發現了什麼是錯的。現在就試試。 –

0

什麼做到這一點path包含,因爲如果它包含一個相對URL,然後從路徑中刪除/。由於您在uri結束時已經有/

+0

路徑包含company.png的名稱,例如:cnn.png – thechmodmaster