首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从隔离存储保存和检索映像时出错

从隔离存储保存和检索映像时出错
EN

Stack Overflow用户
提问于 2012-06-20 11:30:35
回答 2查看 718关注 0票数 0

我试图从URL获取图像,并将其保存到隔离存储中,而不是从隔离存储中获取图像,并在我的WP-7应用程序中显示它。

这是相关代码:

代码语言:javascript
复制
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); I在这个方法上得到了一个异常,GetResourceStream() 预期的相对Uri,找到了绝对的.

我不知道该给这个方法什么,而不是imageUri

提前谢谢!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-20 11:39:52

我想这可能就是你需要的。

代码语言:javascript
复制
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); 
        } 
    }
}
票数 2
EN

Stack Overflow用户

发布于 2012-06-20 11:47:51

这个path包含什么,因为如果它包含一个相对的url,那么从路径中删除/。因为您已经在uri的末尾有/了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11118570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档