从网上下载图像并存储在媒体库中(windows phone 8)

本文关键字:windows phone 媒体库 下载 图像 存储 | 更新日期: 2023-09-27 18:02:34

我正试图从网络上下载一个图像保存在媒体库中,下面是我的代码,我在这里错过了什么,提前感谢

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

从网上下载图像并存储在媒体库中(windows phone 8)

add event handler

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

然后在bitmapImage_DownloadProgress,创建WritableBitMap并保存

private void LoadIMG()
    {
        var bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };

        bitmapImage.ImageOpened += bitmapImage_ImageOpened;
        bitmapImage.ImageFailed += bitmapImage_ImageFailed;
        bitmapImage.DownloadProgress += bitmapImage_DownloadProgress;
        bitmapImage.UriSource = new Uri("http://ds.serving-sys.com/BurstingRes///Site-16990/Type-0/7b912e70-352a-454f-8ea7-5d5ecd6ebfae.gif");
    }
    private void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
    {
    }
    private void bitmapImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
    }
    private void bitmapImage_ImageOpened(object sender, RoutedEventArgs e)
    {
         var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication(); 
         var writeableBitmap = new WriteableBitmap(sender as BitmapImage);
         var isolatedStorageFileStream = userStoreForApplication.CreateFile("Myfile.gif");
         writeableBitmap.SaveJpeg(isolatedStorageFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85); 
    }

你可以试试:http://www.kunal-chowdhury.com/2012/02/how-to-download-and-save-images-in-wp7.html#.TzUtWBSaLkM.twitter