不确定原因,两天尝试不同的事情,我无处可去。继续在WriteableBitmap行获取NRP。你可以看到我试图关闭和冲洗(和两者)流 任何想法都将不胜感激。
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
XDocument document = XDocument.Load(myIsolatedStorage.OpenFile("Selections.xml", FileMode.Open));
MyClass enclaves = (MyClass)serializer.Deserialize(document.CreateReader());
enclavesList.ItemsSource = enclaves.Collection1;
foreach (XElement xencl in document.Descendants("rest"))
{
WebClient downloader = new WebClient();
String theelement = xencl.Element("couplink").Value;
String nameElement = xencl.Element("coup").Value;
String uriring = theelement.ToString();
Uri uri = new Uri(uriring, UriKind.RelativeOrAbsolute);
downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(enclavesDownloaded);
downloader.DownloadStringAsync(uri);
Random random = new Random();
int randomNumber = random.Next(0, 100);
using (IsolatedStorageFile newIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
String tempJPEG = randomNumber.ToString();
IsolatedStorageFileStream fileStream = newIsolatedStorage.CreateFile(tempJPEG);
//fileStream.Close();
//fileStream.Flush();
BitmapImage image = new BitmapImage(new Uri("" + uri ));
image.CreateOptions = BitmapCreateOptions.None;
WriteableBitmap wb = new WriteableBitmap(image);
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
}
}
}
我用Google搜索,直到我失明,不知道现在该做什么。 提前感谢所有人。
答案 0 :(得分:0)
在“new BitmapImage”行之前向图像添加处理程序,如下所示:
this.Image.ImageOpened += ImageOpened;
this.Image.ImageFailed += ImageFailed;
然后,在ImageOpened事件中,保存到WriteableBitmap:
private void ImageOpened(object sender, RoutedEventArgs e)
{
WriteableBitmap wb = new WriteableBitmap((BitmapImage)sender);
using (IsolatedStorageFile newIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
String tempJPEG = randomNumber.ToString();
IsolatedStorageFileStream fileStream = newIsolatedStorage.CreateFile(tempJPEG);
//fileStream.Close();
//fileStream.Flush();
BitmapImage image = new BitmapImage(new Uri("" + uri ));
image.CreateOptions = BitmapCreateOptions.None;
System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
}
}
您当前正在尝试在加载图像之前保存图像,因此空指针异常。
答案 1 :(得分:0)
为尝试将多个图像下载到隔离存储的所有人发布此信息,并在WP上保留文件的名称。请注意,它需要一个url路径,其中包含首次下载的xml文件中的文件名,删除路径,然后使用原始名称保存文件。 借用了一些代码(感谢所有人,特别是philorube),写了其他的并且诅咒了很多来到这里,但它确实有效。
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
XDocument document = XDocument.Load(myIsolatedStorage.OpenFile("Selections.xml", FileMode.Open));
MyClass enclaves = (MyClass)serializer.Deserialize(document.CreateReader());
foreach (var xe in document.Descendants("couplink"))
{
mane = xe.Value.ToString();
WebClient webClient = new WebClient();
Uri uri = new Uri(mane, UriKind.RelativeOrAbsolute);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync((uri),mane);
}
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
int count;
Stream stream = e.Result;
byte[] buffer = new byte[1024];
String imgName = (string)e.UserState;
String cleanImgName = System.IO.Path.GetFileName(imgName);
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (System.IO.IsolatedStorage.IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(cleanImgName, FileMode.Create, isf))
{
count = 0;
while (0 < (count = stream.Read(buffer, 0, buffer.Length)))
{
isfs.Write(buffer, 0, count);
}
stream.Close();
isfs.Close();
}
}