Abdalla
Abdalla

Reputation: 31

Loading animated GIF in C#

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:

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

Upvotes: 3

Views: 2201

Answers (1)

DaMachk
DaMachk

Reputation: 643

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...

Upvotes: 4

Related Questions