OutOfMemory Exception when loading lots of Images from Isolated storage

c#, silverlight, windows-phone-7

Solution

You are getting the OutOfMemory Exception because you are storing all the images in memory at the same time in order to create your StoryBoard. I don't think you will be able to overcome the uncompressed bitmap size that the images require to be displayed on screen.

So to get past this we must think about your goal rather than trying to fix the error. If your goal is to show a new image in sequence every X ms then you have a few options.

Keep using StoryBoards but chain them using the OnCompleted event. This way you don't have to create them all at once but can just generate the next few. It might not be fast enough though if you're changing images every 100ms.

Use CompositionTarget.Rendering as mentioned in my answer here. This would probably take the least amount of memory if you just preload the next one (as opposed to having them all preloaded as your current solution does). You'd need to manually check the elapsed time though.

Rethink what you're doing. If you state what you are going after people might have more alternatives.

Problem

EDIT: I keep getting OutOfMemoryException was unhandled, I think it's how I am saving the image to isolated storage ,I think this is where I can solve my problem how do I reduce the size of the image before I save it? (added code where I save Image) I am opening images from Isolated storage sometimes over 100 images and I want to loop over them images but I get a `OutOfMemory Exception` when there is around 100 to 150 images loaded in to a storyboard. How can I handle this exception, I have already brought down the resolution of the images. How can I handle this exception and stop my app from crashing? I get the exception at this line here `image.SetSource(isStoreTwo.OpenFile(projectFolder + "\\MyImage" + i + ".jpg", FileMode.Open, FileAccess.Read));//images from isolated storage ` here's my code ``` private void OnLoaded(object sender, RoutedEventArgs e) { IsolatedStorageFile isStoreTwo = IsolatedStorageFile.GetUserStoreForApplication(); try { storyboard = new Storyboard { //RepeatBehavior = RepeatBehavior.Forever }; var animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, projectImage); Storyboard.SetTargetProperty(animation, new PropertyPath("Source")); storyboard.Children.Add(animation); for (int i = 1; i <= savedCounter; i++) { BitmapImage image = new BitmapImage(); image.SetSource(isStoreTwo.OpenFile(projectFolder + "\\MyImage" + i + ".jpg", FileMode.Open, FileAccess.Read));//images from isolated storage var keyframe = new DiscreteObjectKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(100 * i)), Value = image }; animation.KeyFrames.Add(keyframe); } } catch (OutOfMemoryException exc) { //throw; } Resources.Add("ProjectStoryBoard", storyboard); storyboard.Begin(); } ``` EDIT This is how I am saving the image to Isolated storage, I think this is where I can solve my problem, How do I reduce the size of the image when saving it to isolated storage? ``` void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e) { string fileName = folderName+"\\MyImage" + savedCounter + ".jpg"; try { // Save picture to the library camera roll. //library.SavePictureToCameraRoll(fileName, e.ImageStream); // Set the position of the stream back to start e.ImageStream.Seek(0, SeekOrigin.Begin); // Save picture as JPEG to isolated storage. using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write)) { // Initialize the buffer for 4KB disk pages. byte[] readBuffer = new byte[4096]; int bytesRead = -1; // Copy the image to isolated storage. while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0) { targetStream.Write(readBuffer, 0, bytesRead); } } } } finally { // Close image stream e.ImageStream.Close(); } } ``` I would appreciate if you could help me thanks.

Original source