从Windows Phone中的内存加载图像

时间:2014-02-04 12:23:20

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

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

我已经能够使用代码

显示它了
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'

2 个答案:

答案 0 :(得分:1)

如果要在Windows Phone设备中保存图像,则需要使用IsolatedStorage。 保存图片=>

            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;

答案 1 :(得分:0)

如果您想从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);
                 }
              }
           }
       }
}