0

不知道为什么,两天尝试不同的事情我一无所获。继续在 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);
              }
           }
         }

我用谷歌搜索直到我失明,不知道现在该做什么。提前谢谢大家。

4

2 回答 2

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);
          }
}

您当前正试图在图像加载之前保存图像,因此出现空指针异常。

于 2014-01-29T17:28:12.810 回答
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();
            }
        }
于 2014-02-04T22:59:38.910 回答