0

I got a OutOfmemory error when i create a list of bitmapimage...

What am i supposed to do...?

Thanks for your help ;)

Here is my code :

     foreach (var bytearray in imageDataBlocksPresta)
            {
                if (bytearray != null)
                {
                    MemoryStream ms;
                    using (ms = new MemoryStream(bytearray, 0, bytearray.Length))
                    {
                        BitmapImage photo = new BitmapImage();
                        photo.DecodePixelHeight = 800;
                        photo.DecodePixelWidth = 624;
                        photo.SetSource(ms);//ERROR

                        listphotoPresta.Add(photo);
                    }
                }

                else//si photo null
                {
                    BitmapImage photo = new BitmapImage();
                    photo.DecodePixelHeight = 800;
                    photo.DecodePixelWidth = 624;
                    photo.UriSource = new Uri("/Images/NoImageIcon.jpg", UriKind.RelativeOrAbsolute);

                    listphotoPresta.Add(photo);

                }
2
  • How many byte arrays do you have? Does the error occur in the first foearch iteration or later on (when)? Commented Dec 13, 2013 at 16:52
  • the error occurs when several photos have already been added to the list. The length of the byte arrays is between 30k and 40k.It's not the photo size which makes that the error happen but mostly the sum of all. Commented Dec 16, 2013 at 9:08

2 Answers 2

2

Try setting photo to null and calling GC.Collect() once you've added it. Like this:

listphotoPresta.Add(photo);

photo = null;
GC.Collect();
Sign up to request clarification or add additional context in comments.

Comments

0
  • Don't create NoImageIcon all the time. Use only one reference
  • Don't try to store all images in memory. It's a mobile.
  • Limit your app for devices with at least 512MB RAM
  • Store lower resuolution pictures. 800*600 is full screen on many devices
  • Load images on demand
  • Call GC right after you release an image

1 Comment

the object named "photo" is not my UI object...I have a model with a bitmapimage property inside. I got a list of instances and a list of bitmapimage.After i assign each object from the bitmapimage list to object in the model object list...Sry for my english

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.