Display animated GIFs and change them at run time in C#

display animated GIFs

You can display animated GIFs by setting the Image property of a Button, PictureBox, Label, or other control to the GIF file. (If you display one in a Form‘s BackgroundImage property, you don’t get the animation.)

There are a couple of ways to change GIFs at runtime.

First, you can add the GIF as a resource. Open the Project menu and select Properties at the bottom. On the Resources tab, open the Add Resource dropdown and select Add Existing File. Browse to the GIF and click Open.

Now you can use the GIF resource as in:

picGif.Image =
    howto_display_animated_gif.Properties.Resources.puppy;

You can also load GIFs from a file as in the following code.

lblGif.Image = Image.FromFile(filename + "alien.gif");

This program uses resources to change GIFs when you click on its Button or PictureBox. It loads GIFs from files when you click its Label.


Download Example   Follow me on Twitter   RSS feed




This entry was posted in graphics, user interface and tagged , , , , , , , , , . Bookmark the permalink.

8 Responses to Display animated GIFs and change them at run time in C#

  1. Pascal says:

    I have created a DLL which can display an animated GIF in a window of a dialog box, which also works for Visual C++.

    • RodStephens says:

      Do you want to post a link?

      • pascal says:

        This is the link to a complete project, sources included, which contains this DLL and uses it:

        http://www.angelfire.com/moon2/xpascal/Applications/GifDemo.Zip

        The principle is quite simple: The user loads an animated GIF by calling a procedure “LoadGif”, in which he gives the full pathname of the GIF.
        The procedure returns a pointer to a structure which allows to handle the GIF (which contains the sizes of the GIF, its number of pictures, its temporizations and an array of raw bitmaps representing the pictures of the GIF).
        The user must keep this pointer, it represents his Gif; when he has finished using the Gif, he calls a procedure FreeGif with this pointer.
        Then he has a whole set of procedures which allow to handle this Gif in various ways, to which he gives the pointer of the GIF and parameters.
        He can display any picture of the GIF in a window of a dialog box, and he can also automatically sequence the pictures of the GIF.
        In order to automatically sequence the pictures of the GIF, either a manual way or an automatic way can be used.
        The manual way consists in attributing a Timer event to the GIF, and arming the timer on this event; each time this event elapses in the timer, the user calls a procedure of the DLL allowing to display the next picture of the gif, and this procedure returns the temporization to display this picture, and the user rearms the timer event with this temporization.
        The automatic way is simpler to use: The user just calls a procedure to make the DLL automatically periodically display the pictures of the GIF; in that case the timer of the project is not used, the DLL starts a process which takes care of the automatic sequential display of the pictures; the user does not release the context it gives to the procedure of the DLL which makes the automatic sequential display and in which the process started by the DLL displays the pictures of the Gif, for the DLL releases it itself when the procedure to stop the automatic display is called.
        In the previous version, there only was the manual way to sequence the display of the pictures of the GIF; I have recently added the automatic way, to simplify the use of the animated GIF (and it runs more smoothly).
        I am a professional computer engineer.

      • pascal says:

        Could you give a feedback to my last comment, please?

Leave a Reply

Your email address will not be published. Required fields are marked *