【问题标题】:Loading Image from memory in Windows Phone在 Windows Phone 中从内存中加载图像
【发布时间】:2014-02-04 12:23:20
【问题描述】:

我希望能够拍照、显示并保留位置,以便我可以将其保存到记录中并能够在以后显示。

我已经能够使用代码很好地显示它

BitmapImage bmp = newBitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp2;

当 myImage 是正在显示的图像,并且 e 是 PhotoResult 对象时。但是,由于我需要将其保存在记录中,因此我尝试使用此代码根据位置显示照片。

string imageLoc = e.OriginalFileName;
Uri imageUri = new Uri(imageLoc, UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);
BitmapImage bmp = BitmapImage();
bmp.SetSource(resourceInfo.Stream);
myImage.Source = bmp;

当我运行此代码时,我得到一个 System.NullReferenceException。我认为这与 Application.GetResourceStream 有关,但我不确定发生了什么问题。

为澄清起见,我希望能够从某个位置加载和显示照片,例如 'C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg'

【问题讨论】:

    标签: c# windows-phone-7 windows-phone-8 mobile-application


    【解决方案1】:

    如果您想将图像保存在 Windows Phone 设备中,您需要使用独立存储。 保存图片 =>

                String tempJPEG = "logo.jpg";
    
                // Create virtual store and file stream. Check for duplicate tempJPEG files.
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists(tempJPEG))
                    {
                        myIsolatedStorage.DeleteFile(tempJPEG);
                    }
    
                    IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
    
    
                    StreamResourceInfo sri = null;
                    Uri uri = new Uri(tempJPEG, UriKind.Relative);
                    sri = Application.GetResourceStream(uri);
    
                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(sri.Stream);
                    WriteableBitmap wb = new WriteableBitmap(bitmap);
    
                    // Encode WriteableBitmap object to a JPEG stream.
                    Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
    
                    //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                    fileStream.Close();
                }
    

    读取图片 =>

    BitmapImage bi = new BitmapImage();
    
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
                    {
                        bi.SetSource(fileStream);
                        this.img.Height = bi.PixelHeight;
                        this.img.Width = bi.PixelWidth;
                    }
                }
                this.img.Source = bi;
    

    【讨论】:

    • 我的照片会自动保存在“C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg”的相机胶卷中,是否需要第一段代码?
    • Windows phone 或 windows 8。使用 windows phone 时,无法通过此路径访问图片。当您使用您的应用程序代码拍照时,图片会使用此路径 'C:\Data\Users\Public\Pictures\Camera Roll\imageExample.jpg' 保存?
    • 只是为了测试这样做:string imageLoc = e.OriginalFileName; Uri imageUri = new Uri(imageLoc, UriKind.Relative); StreamResourceInfo resourceInfo = Application.GetResourceStream(imageUri);位图图像 bmp = 位图图像(); bmp.UriSource = imageUri; myImage.Source = bmp;
    【解决方案2】:

    如果您想从 MediaLibrary 中获取图片(相机胶卷、保存的图片 ..),那么您可以完成您的任务:

    您的代码可能如下所示(我已将其编辑为仅使用相机胶卷中的图像):
    您可以使用 line picture.GetImage() 获取图片流 - 此方法返回 Stream,您可以使用它来复制到 IsolatedStorage。

    private void MyImg()
    {
        using (MediaLibrary mediaLibrary = new MediaLibrary())
           foreach (PictureAlbum album in mediaLibrary.RootPictureAlbum.Albums)
           {
               if (album.Name == "Camera Roll")
               {
                 PictureCollection pictures = album.Pictures;
                 foreach (Picture picture in pictures)
                 {
                     // example how to use it as BitmapImage
                     // BitmapImage image = new BitmapImage();
                     // image.SetSource(picture.GetImage()); 
                     // I've commented that above out, as you can get Memory Exception if
                     // you try to read all pictures to memory and not handle it properly.
    
                     // Example how to copy to IsolatedStorage
                     using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                     {
                        if (!storage.DirectoryExists("SavedImg"))
                            storage.CreateDirectory("SavedImg");
    
                        if (storage.FileExists("SavedImg" + @"\" + picture.Name))
                            storage.DeleteFile("SavedImg" + @"\" + picture.Name);
                        using (IsolatedStorageFileStream file = storage.CreateFile("SavedImg" + @"\" + picture.Name))
                            picture.GetImage().CopyTo(file);
                     }
                  }
               }
           }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      相关资源
      最近更新 更多