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