我试图从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); I在这个方法上得到了一个异常,GetResourceStream() 预期的相对Uri,找到了绝对的.。
我不知道该给这个方法什么,而不是imageUri。
提前谢谢!!
发布于 2012-06-20 11:39:52
我想这可能就是你需要的。
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-20 11:47:51
这个path包含什么,因为如果它包含一个相对的url,那么从路径中删除/。因为您已经在uri的末尾有/了。
https://stackoverflow.com/questions/11118570
复制相似问题