Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将照片(CameraCaptureTask)保存到独立存储-OutOfMemoryException_C#_Windows Phone 7_Out Of Memory_Cameracapturetask - Fatal编程技术网

C# 将照片(CameraCaptureTask)保存到独立存储-OutOfMemoryException

C# 将照片(CameraCaptureTask)保存到独立存储-OutOfMemoryException,c#,windows-phone-7,out-of-memory,cameracapturetask,C#,Windows Phone 7,Out Of Memory,Cameracapturetask,我想将在我的应用程序中拍摄的照片(使用CameraCaptureTask)保存到独立存储中。当前的问题是RAM的消耗,这总是导致OutOfMemoryException。当我将图片加载到图像控件时也会发生这种情况 我的应用程序最终应该能够拍摄10张照片,将它们保存到单独的存储中,在图像控件中显示它们,并在必要时永久删除照片 降低图片的分辨率在逻辑上解决了这个异常,但这不是我想要的方式 也许你能给我一个提示 这是我的密码: private CameraCaptureTask ccTask = ne

我想将在我的应用程序中拍摄的照片(使用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;
        }
非常感谢,如果有任何不清楚的地方,请随时询问。也许有更简单的方法来保存照片,我只是从应用程序开发开始 你好,丹尼尔


顺便说一句:1600x1200是2MP,我降低了分辨率以避免异常,不幸的是它只是被延迟了

10张分辨率为1600*1200的图片使用了大约80MB的ram。Windows Phone 7上的内存限制为90 MB,Windows Phone 8上的内存限制为150 MB,您尝试执行的操作无法正常工作

我的应用程序最终应该能够拍摄10张照片,将它们保存到单独的存储中,在图像控件中显示它们,并在必要时永久删除照片


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

我听从了你的建议,它似乎起了作用。现在,我正在监视RAM的使用情况(请参阅),我意识到它经常几乎达到内存限制,但是当前的使用情况从一个操作下降到下一个操作。这是正常行为吗?@Danielabouchleich这就是在Windows Phone和每个.NET应用程序上管理内存的方式。有一个进程在后台运行,称为垃圾收集器。在某些情况下(如内存不足),会触发一个集合,它将释放所有不再使用的内存。您可以通过调用
GC.Collect()
手动触发收集,但这通常不是必需的。如何将大小>150 MB的文件从远程位置直接下载到独立存储,而不出现任何内存不足异常?请参阅此处的详细信息