内存不足...在Windows Phone 8中

时间:2013-08-19 05:41:04

标签: c# .net windows-phone-7 windows-phone-8

我正试图从Windows Phone 8的照片库中获取图像,并且它第一次正常工作。我第一次从相机胶卷文件夹中获取图像,但是当我尝试从相册“保存的图片”中拍摄图片时,它会抛出“内存异常”...我不知道为什么会发生这种情况。任何帮助表示赞赏。

    MediaImage mediaImage = new MediaImage();


    BitmapImage image;
    private void Panorama_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        Panorama obj = sender as Panorama;
        PanoramaItem objPanoramaItem = (PanoramaItem)obj.SelectedItem;
        string FolderName = objPanoramaItem.Header.ToString();
        PictureAlbum AlbumFolder = allAlbums.Where(album => album.Name == FolderName).FirstOrDefault();
        if (FolderName == "Camera Roll")
        {
            if (ImageListCameraRoll == null)
            {
                ImageListCameraRoll = new ObservableCollection<MediaImage>();



                var CameraRollPictures = AlbumFolder.Pictures;

                foreach (var picture in CameraRollPictures)
                {
                    mediaImage = new MediaImage();
                    image = new BitmapImage();
                    image.SetSource(picture.GetImage());
                    mediaImage.ImageFile = image;
                    mediaImage.ImageName = picture.Name;
                    ImageListCameraRoll.Add(mediaImage);
                }
            }
            ListboxCameraRoll.ItemsSource = ImageListCameraRoll;
        }
        if (FolderName == "Saved Pictures1")
        {
            if (ImageListSavedPictures == null)
            {
                ImageListSavedPictures = new ObservableCollection<MediaImage>();
                var SavedPictures = AlbumFolder.Pictures;
                foreach (var picture in SavedPictures)
                {
                    mediaImage = new MediaImage();
                    image = new BitmapImage();
                    image.SetSource(picture.GetImage());
                    mediaImage.ImageFile = image;
                    mediaImage.ImageName = picture.Name;
                    ImageListSavedPictures.Add(mediaImage);
                }
            }
            ListboxSavedPictures.ItemsSource = ImageListSavedPictures;
        }
}

我在这里做的是试图在全​​景项目更改事件上获取图片。全景项目基于照片库中的不同文件夹..

1 个答案:

答案 0 :(得分:5)

将文件夹中的所有图像加载到内存中可能会占用大量内存空间。除非您真的需要全尺寸图片,否则我建议您使用GetThumbnail()来加载照片的低分辨率版本。一旦用户选择了要执行操作的照片,您就可以一直返回到原件。

image.SetSource(picture.GetThumbnail());