我正在尝试从网络上下载图像,将其保存在媒体库中,以下是我的代码,我在这里是否缺少任何内容,请提前感谢

  public void storePicture()
    {
        try
        {


            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {

                 string url = @"http://mynokiablog.com/wp-content/uploads/2012/11/wp8.jpeg";

                  BitmapImage storeimage = new BitmapImage(new Uri(url));

                  // height and width are 0
                  int testheight = storeimage.PixelHeight;
                  int testwidth = storeimage.PixelWidth;
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile("testname");
                  // NullRefrenceException
                WriteableBitmap wb = new WriteableBitmap(storeimage);

                wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                fileStream.Close();
            }

        }
        catch (Exception ex)
        {
               System.Diagnostics.Debug.WriteLine(ex.Message);
        }

    }

最佳答案

如下添加事件处理程序

    storeimage.ImageOpened += bitmapImage_ImageOpened;
    storeimage.ImageFailed += bitmapImage_ImageFailed;
    storeimage.DownloadProgress += bitmapImage_DownloadProgress;


然后在bitmapImage_DownloadProgress,中创建WritableBitMap并保存

10-05 23:15