Frage

I would like to save a photo, taken within my App(using CameraCaptureTask), to the isolated storage. The current Problem is the consumption of RAM, which always leads to an OutOfMemoryException. This also happens when I am loading the picture into an Image-Control.

My App should finally be able to take 10 pictures, save them to isolated storage, show them in an Image Control and if necessary delete the picture for good.

Lowering the resolution of the pictures logically fixed the exception, but that's not the way I wanted to go.

Maybe you can give me a hint.

Here is my code:

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;
        }

Thanks a lot, if there is anything unclear, feel free to ask. Maybe there is a much easier way to save a photo, I am just beginning with app development Greetings Daniel

Btw: 1600x1200 are 2MP, I lowered the resolution to avoid the exception, unfortunately it was just delayed

War es hilfreich?

Lösung

10 pictures with a resolution of 1600 * 1200 use about 80MB of ram. The memory limit is 90 MB on Windows Phone 7 and 150 MB on Windows Phone 8, there's no way what you're trying to do could work.

My App should finally be able to take 10 pictures, save them to isolated storage, show them in an Image Control and if necessary delete the picture for good.

This approach is correct, however you are loading the full-sized picture to display in the thumbnails, which is a total waste of RAM. When you're saving the picture to the isolated storage, save a copy with a lower resolution and display that copy in the thumbnail. Then, when the user tap on a thumbnail, load the full-res picture from the isolated storage to display it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top