3

I created a simple animated GIF using Adobe Flash CS3, the GIF contains 200 frames (1280x786). When I load it to be displayed in a C# WPF application, the program gets an OutofMemoryException on the following code

private void set_gif_Image(String path)
    {
        if (File.Exists(path))
        {
            var bitmapimage = new BitmapImage();

            bitmapimage.BeginInit();
            bitmapimage.UriSource = new Uri(path);
            bitmapimage.EndInit();
            ImageBehavior.SetAnimatedSource(img_preview, bitmapimage);//Exception Here
        }
        else
        {
            var bitmapimage = new BitmapImage();

            bitmapimage.BeginInit();
            bitmapimage.UriSource = new Uri("C:\\testing_files\\ERROR.gif");
            bitmapimage.EndInit();

            ImageBehavior.SetAnimatedSource(img_preview, bitmapimage);
        }

But if I load a 20 frame GIF for example, the program loads fine. Notice the following Situations:

  • 1280x768 with 200 frames: Exception
  • 550x400 with 200 frames: Loads fine
  • 1280x768 with 50 frames: Loads fine

Where is the problem? how to get such large animated GIF loaded without an exception?

Abdalla
  • 31
  • 2
  • I hope this SO post can help you. http://stackoverflow.com/questions/210922/how-do-i-get-an-animated-gif-to-work-in-wpf – Nitesh Aug 03 '13 at 15:10
  • It is probably best to assume the exception is accurate. Consuming 786 megabytes of memory is certainly deep in the danger zone for a 32-bit process. You'll need a 64-bit process to get ahead. Change the Platform target setting for your EXE project to AnyCPU. A 64-bit version of Windows is required. – Hans Passant Aug 03 '13 at 15:20
  • I have already a 64-bit system (intel core i7, 6GB of RAM) :| – Abdalla Aug 03 '13 at 16:04

1 Answers1

4

Or you can run it in Release mode... Shouldn't be bothered with the OutOfMemoryException...

I do advise that you resize the GIF a bit, because a 200fps 1280x768 bitmap represents an awful amount of memory...

Let's see... 1 frame represents 983040 pixels... (1280 x 768), each pixel is made of data for 3 colors, so 3 Bytes equals: 3 * 983040 = 2949120 Bytes for only one frame... YOu have 200 of those... so: 589824000 Bytes. That equals to 576000 kBytes or 562.7 MBytes...

That's an awful lot...

DaMachk
  • 643
  • 4
  • 10
  • 1
    Don't forget the alpha value. Each pixel takes 4 bytes. That's 786432000 bytes or 750 MB in total, as Hans Passant has already written in his comment to the question. And how would running in Release mode prevent the application from running out of memory? – Clemens Aug 03 '13 at 16:23
  • Yeah, i wasn't sure if the Bitmap has the alpha value representation. Haven't used it in a while. Running it in release mode will leave the "debug reserved" memory and take any avalible memory to use. I had an app that threw the OOMException when it used 1,5GB of memory in debug mode. Once i put it in release mode it chomped up the avalible memory and grew to 6GB with no warnings and did it's job (leave aside that using 6GB of memory isn't good, as i abandoned that approach later). – DaMachk Aug 03 '13 at 16:35
  • I'm still having the same issue issue even in release mode :| I just don't know where is the insufficient memory on a 6 GB RAM machine :| – Abdalla Aug 04 '13 at 09:24
  • I think there is a problem with library i'm using, an 1024x768 works fine on release mode configuration. I've heard there are software developers who creates custom flash animation and use it as an animated GIF in an applicaation with even larger number of frames and the applications works fine. Any other way to load animated GIF? – Abdalla Aug 04 '13 at 11:57