将照片(CameraCaptureTask)保存到隔离存储 - OutOfMemoryException

时间:2013-07-17 19:53:46

标签: c# windows-phone-7 out-of-memory cameracapturetask

我想将我的应用程序(使用CameraCaptureTask)中拍摄的照片保存到隔离存储中。当前的问题是RAM的消耗,它总是导致 OutOfMemoryException 。当我将图片加载到图像控制中时也会发生这种情况。

我的应用程序应该最终可以拍摄10张照片,将它们保存到独立的存储空间,在图像控件中显示它们,如有必要,还可以删除照片。

降低图片的分辨率逻辑上修复了异常,但这不是我想要的方式。

也许你可以给我一个提示。

这是我的代码:

private CameraCaptureTask ccTask = new CameraCaptureTask();
WriteableBitmap[] imgList = new WriteableBitmap[10];
Random rnd = new Random();

private void addPicture_button_Click(object sender, EventArgs e)
{
    ccTask.Show();
    ccTask.Completed += ccTask_Completed;
}

void ccTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            WriteableBitmap writeableBitmap = new WriteableBitmap(1600,1200);
            writeableBitmap.LoadJpeg(e.ChosenPhoto);

            string imageFolder = "Unfaelle";
            string datetime = DateTime.Now.ToString().Replace("/","");
            datetime = datetime.Replace(":","");
            string imageFileName = "Foto_"+datetime+".jpg";
            using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {

                if (!isoFile.DirectoryExists(imageFolder))
                {
                    isoFile.CreateDirectory(imageFolder);
                }

                string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
                using (var stream = isoFile.CreateFile(filePath))
                {
                    writeableBitmap.SaveJpeg(stream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
                }
            }

            //now read the image back from storage to show it worked...
            BitmapImage imageFromStorage = new BitmapImage();

            using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
                using (var imageStream = isoFile.OpenFile(
                    filePath, FileMode.Open, FileAccess.Read))
                {
                    imageFromStorage.SetSource(imageStream);
                }
            }

            Rectangle b = new Rectangle()
            {
                Width = 100,
                Height = 100,
            };

            Thickness margin = b.Margin;
            margin.Left = 10;
            margin.Top = 10;
            b.Margin = margin;

            ImageBrush imgBrush = new ImageBrush();
            imgBrush.ImageSource = imageFromStorage;
            b.Fill = imgBrush;
            b.Tag = System.IO.Path.Combine(imageFolder, imageFileName);
            b.Tap += OnTapped;

            pictures_wrapPanel.Children.Add(b);
        }
    }

private void OnTapped(object sender, System.Windows.Input.GestureEventArgs e)
        {

            Rectangle r = sender as Rectangle;
            BitmapImage imageFromStorage = new BitmapImage();

            using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string filePath = r.Tag.ToString();
                using (var imageStream = isoFile.OpenFile(
                    filePath, FileMode.Open, FileAccess.Read))
                {
                    imageFromStorage.SetSource(imageStream);
                }
            }
            img.Source = imageFromStorage;
        }

非常感谢,如果有什么不清楚,请随时问。也许有一种更简单的方法来保存照片,我刚开始使用应用程序开发 问候丹尼尔

Btw:1600x1200是2MP,我降低了分辨率以避免异常,不幸的是它只是被推迟了

1 个答案:

答案 0 :(得分:3)

分辨率为1600 * 1200的10张照片使用约80MB的RAM。 Windows Phone 7上的内存限制为90 MB,Windows Phone 8上的内存限制为150 MB,您尝试做的事情无法正常工作。

  

我的应用程序应该最终可以拍摄10张照片,将它们保存到独立的存储空间,在图像控件中显示它们,如有必要,还可以删除照片。

这种方法是正确的,但是您要加载全尺寸图片以在缩略图中显示,这完全是浪费RAM。将图片保存到隔离存储时,请保存较低分辨率的副本,并在缩略图中显示该副本。然后,当用户点击缩略图时,从隔离的存储中加载完整图像以显示它。

相关问题