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() 预期的相对 Uri,发现是绝对的。

我不明白用什么方法代替imageUri.

提前致谢!!

4

2 回答 2

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); 
        } 
    }
}
于 2012-06-20T11:39:52.233 回答
0

path包含什么,因为如果它包含相对 url,/则从路径中删除。因为你已经/在 uri 结尾了。

于 2012-06-20T11:47:51.457 回答