我正在尝试从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
。
提前致谢!!
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:0)
此path
包含什么,因为如果它包含相对网址,则从路径中删除/
。由于你在uri的末尾已经有/
。