Click here to Skip to main content
13,296,377 members (57,899 online)
Click here to Skip to main content
Add your own
alternative version


112 bookmarked
Posted 29 Jan 2002

Adding GIF-animation using GDI+

, 29 Jan 2002
Rate this:
Please Sign up or sign in to vote.
Norm demonstrates how to coerce GDI+ into displaying animated GIFs


How many of you developers are using GDI+? I reckon, not many. As a seasoned Windows C++ programmer developing for desktop applications, I've been  using the archaic GDI on regular basis. Since the release of GDI+ (see ( I've been using GDI+ in new applications and also converting existing applications to use GDI+. There numerous compelling reasons why you should be using GDI+, here are some:

  • Successor to GDI
  • Compatible with .NET
  • Optimizes many of the capabilities of GDI
  • Supports: Gradient Brushes, Independent Path Objects, Transformations and the Matrix Object, Scalable Regions, Alpha Blending and support for multiple image formats:
    • BMP
    • GIF
    • JPEG
    • Exif
    • PNG
    • TIFF
    • ICON
    • WMF
    • EMF

During my development of Windows GUI based applications with GDI+, I've come across the need to display animated GIFs, whilst GDI+ does not support displaying animated GIFs directly, in can be done with a little coding.


Firstly lets look at drawing a simple image using GDI+.

void CMyWnd::OnPaint()
	CPaintDC dc(this);
	Graphics graphics(&dc); // Create a GDI+ graphics object

	Image image(L"Test.Gif"); // Construct an image
	graphics.DrawImage(&image, 0, 0, image.GetWidth(), image.GetHeight());	

Straight away we can see the simplistic C++ interface for using GDI objects. This makes it a sure fire way for using GDI objects. There is no need for using

to select GDI objects in and out of the device context.

For those of the readers who are not familiar with the format of an animated GIF, animated GIF is actually a series of GIF frames with a delay time associated each frame, therefore each frame can have a different delay time.

My implementation of encapsulating an animated GIF functionality derives a class called from ImageEx from the GDI+ Image class. The first task is to determine whether or not a GIF is of the animated kind. The following code demonstrates this:

bool ImageEx::TestForAnimatedGIF()
	UINT count = 0;
	count = GetFrameDimensionsCount();
	GUID* pDimensionIDs = new GUID[count];

	// Get the list of frame dimensions from the Image object.
	GetFrameDimensionsList(pDimensionIDs, count);

	// Get the number of frames in the first dimension.
	m_nFrameCount = GetFrameCount(&pDimensionIDs[0]);

	// Assume that the image has a property item of type PropertyItemEquipMake.
	// Get the size of that property item.
	int nSize = GetPropertyItemSize(PropertyTagFrameDelay);

	// Allocate a buffer to receive the property item.
	m_pPropertyItem = (PropertyItem*) malloc(nSize);

	GetPropertyItem(PropertyTagFrameDelay, nSize, m_pPropertyItem);

	delete pDimensionIDs;

	return m_nFrameCount > 1;
<!-- Info Task Footer -->

m_pPropertyItem->value is actually a pointer to an array of longs, each long is a delay time which corresponds to a GIF frame. Because

returns a different size depending on what property using interested in, a Size is require and its the programmer responsibility to allocate and deallocate the memory associated with GetPropertyItem. The size is determined by calling GetPropertyItemSize supplying property tag your interested in.

Once the number of frames and delay times have been retrieved from the image, a thread is created which calls to

until the object destructs. See DrawFrameGIF below:

bool ImageEx::DrawFrameGIF()

	::WaitForSingleObject(m_hPause, INFINITE);

	GUID pageGuid = FrameDimensionTime;

	long hmWidth = GetWidth();
	long hmHeight = GetHeight();

	HDC hDC = GetDC(m_hWnd);
	if (hDC)
		Graphics graphics(hDC);
		graphics.DrawImage(this, m_rc.left,, hmWidth, hmHeight);
		ReleaseDC(m_hWnd, hDC);

	SelectActiveFrame(&pageGuid, m_nFramePosition++); 

	if (m_nFramePosition == m_nFrameCount)
		m_nFramePosition = 0;

	long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;

	DWORD dwErr = WaitForSingleObject(m_hExitEvent, lPause);

	return dwErr == WAIT_OBJECT_0;

One other interesting aspect of this class is it has the ability of loading a image directly as a resource from an executable. I usually import my GIF into a project and give it a resource type of "GIF" and then rename the resource ID from a numeric constant to a string (see the example code).


If you familiar with the concepts and programming of GDI, GDI+ provides some advance features and the interface almost mirrors the .NET GDI namespace. The sample apps and code include the full listings for implementing animated GIF in your code applications. For code reference just search for GDI+ within the code. One thing I haven't included in this article is starting up and shutting the GDI+ subsystem.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom

Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.

We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight

Visit Software Kinetics

You may also be interested in...


Comments and Discussions

BugA small mistake could lead to Undefined Behavior Pin
Member 112854378-Jun-15 3:50
memberMember 112854378-Jun-15 3:50 
GeneralMy vote of 3 Pin
Mahdi Nejadsahebi30-Apr-11 22:13
groupMahdi Nejadsahebi30-Apr-11 22:13 
Questionone thread per gif, more gif picture will use more thread, and that's terrible Pin
David_LoveCpp7-Oct-10 4:31
memberDavid_LoveCpp7-Oct-10 4:31 
and there will maybe also have some synchronization problem,
besides, GetDC/ReleaseDC may not work in some situation.
for example like ws_ex_composited style windows class, etc...

I think it is better that put all drawing code in wm_paint message handler.
the good news is this can modify very easily.

I will post another way to draw animated gif / apng later.
QuestionProblem with speed for animate gif [modified] Pin
harturo12-Feb-10 12:06
memberharturo12-Feb-10 12:06 
AnswerRe: Problem with speed for animate gif Pin
Garth J Lancaster12-Feb-10 12:58
memberGarth J Lancaster12-Feb-10 12:58 
AnswerRe: Problem with speed for animate gif [modified] Pin
harturo12-Feb-10 17:36
memberharturo12-Feb-10 17:36 
AnswerRe: Problem with speed for animate gif: ===THANKS, DOUBT RESOLVED=== [modified] Pin
harturo16-Feb-10 22:23
memberharturo16-Feb-10 22:23 
GeneralRe: Problem with speed for animate gif: ===THANKS, DOUBT RESOLVED=== Pin
Norm .net16-Feb-10 22:35
groupNorm .net16-Feb-10 22:35 
GeneralRe: Problem with speed for animate gif: ===THANKS, for your quickly 12 min. after Re=== Pin
harturo17-Feb-10 15:22
memberharturo17-Feb-10 15:22 
GeneralRe: Problem with speed for animate gif: ===THANKS, for your quickly 12 min. after Re=== Pin
Norm .net17-Feb-10 21:36
groupNorm .net17-Feb-10 21:36 
AnswerRe: Problem with speed for animate gif Pin
kwoitek4-Nov-10 3:07
memberkwoitek4-Nov-10 3:07 
Generalmultithreaded use Pin
Anant wakode28-Oct-09 1:35
memberAnant wakode28-Oct-09 1:35 
GeneralRe: multithreaded use Pin
Norm .net28-Oct-09 2:05
groupNorm .net28-Oct-09 2:05 
GeneralMy vote of 1 Pin
Sheetal_Joshi22-Oct-09 3:20
memberSheetal_Joshi22-Oct-09 3:20 
GeneralRe: My vote of 1 Pin
Norm .net28-Oct-09 2:02
groupNorm .net28-Oct-09 2:02 
GeneralSerious Error during drawing a Multi-Frame(&gt;10) Gif file Pin
adelezy16-Sep-09 23:07
memberadelezy16-Sep-09 23:07 
GeneralAccess Violation Pin
adelezy15-Sep-09 0:12
memberadelezy15-Sep-09 0:12 
GeneralCommercial License Pin
Rafi Rainshtein21-Jul-09 23:25
memberRafi Rainshtein21-Jul-09 23:25 
GeneralRe: Commercial License Pin
Norm .net21-Jul-09 23:40
groupNorm .net21-Jul-09 23:40 
GeneralRe: Commercial License Pin
Rafi Rainshtein22-Jul-09 2:45
memberRafi Rainshtein22-Jul-09 2:45 
GeneralVC++.Net and Images Pin
minad_7865-Sep-07 4:54
memberminad_7865-Sep-07 4:54 
GeneralRe: VC++.Net and Images Pin
norm .net5-Sep-07 4:59
membernorm .net5-Sep-07 4:59 
GeneralRe: VC++.Net and Images Pin
minad_7866-Sep-07 0:40
memberminad_7866-Sep-07 0:40 
GeneralRe: VC++.Net and Images Pin
norm .net6-Sep-07 3:09
membernorm .net6-Sep-07 3:09 
GeneralCreate animated GIF Pin
Tall Dude19-Jan-07 9:58
memberTall Dude19-Jan-07 9:58 
GeneralRe: Create animated GIF Pin
norm .net25-Jan-07 23:48
membernorm .net25-Jan-07 23:48 
GeneralGDI+ function to identify moving GIF image programatically Pin
mukta_here8-Sep-06 4:31
membermukta_here8-Sep-06 4:31 
QuestionProblem with Image object in GDI+ (VC++) Pin
Dhananjayak0214-Aug-06 5:10
memberDhananjayak0214-Aug-06 5:10 
AnswerRe: Problem with Image object in GDI+ (VC++) Pin
Infro3-Sep-06 12:45
memberInfro3-Sep-06 12:45 
AnswerRe: Problem with Image object in GDI+ (VC++) Pin
ddzh22-Nov-07 23:50
memberddzh22-Nov-07 23:50 
GeneralGDI+ Buffered image Pin
darb29-Aug-06 14:47
memberdarb29-Aug-06 14:47 
GeneralRe: GDI+ Buffered image Pin
norm .net10-Aug-06 0:36
membernorm .net10-Aug-06 0:36 
GeneralMagnificent !!!!! Pin
Hiigara3-Nov-05 4:12
memberHiigara3-Nov-05 4:12 
GeneralRe: Magnificent !!!!! Pin
norm.net8-Apr-06 0:03
membernorm.net8-Apr-06 0:03 
GeneralAnimated GIFs with transparecy Pin
liqutttt13-Apr-05 9:10
sussliqutttt13-Apr-05 9:10 
GeneralRe: Animated GIFs with transparecy Pin
defrog11-Oct-05 5:45
memberdefrog11-Oct-05 5:45 
GeneralRe: Animated GIFs with transparecy Pin
Claudio Sampaio25-Apr-06 12:02
memberClaudio Sampaio25-Apr-06 12:02 
GeneralRe: Animated GIFs with transparecy Pin
game0071-Sep-06 17:30
membergame0071-Sep-06 17:30 
GeneralRe: Animated GIFs with transparecy Pin
KenDang8-Aug-07 6:02
memberKenDang8-Aug-07 6:02 
GeneralRe: Animated GIFs with transparecy (The best way for transparency!!) Pin
Greg Ellis11-Nov-08 12:19
memberGreg Ellis11-Nov-08 12:19 
QuestionRe: Animated GIFs with transparecy (The best way for transparency!!) Pin
Istvan Lengyel27-Nov-08 6:03
memberIstvan Lengyel27-Nov-08 6:03 
GeneralRendering Time of each Frame Pin
Kohaku-Chan20-Dec-04 11:36
memberKohaku-Chan20-Dec-04 11:36 
GeneralRe: Rendering Time of each Frame Pin
TDK90000016-Oct-05 23:00
memberTDK90000016-Oct-05 23:00 
QuestionHow to create a multi-frame .gif file in C#? Pin
jackyxio24-Jul-04 0:36
memberjackyxio24-Jul-04 0:36 
QuestionHow do I change the Color Palette of Gif Image Pin
pubba2-Jul-04 18:04
susspubba2-Jul-04 18:04 
GeneralAnyone tried using this with an MFC dialog application using Visual Studio 2003 Pin
smesser16-May-04 19:13
membersmesser16-May-04 19:13 
GeneralRe: Anyone tried using this with an MFC dialog application using Visual Studio 2003 Pin
defrog11-Oct-05 6:39
memberdefrog11-Oct-05 6:39 
GeneralRe: Anyone tried using this with an MFC dialog application using Visual Studio 2003 Pin
smesser11-Oct-05 16:20
membersmesser11-Oct-05 16:20 
QuestionHow to display .gif file on the toolbar and react to user click ? Pin
Jim Morgan3-May-04 9:31
sussJim Morgan3-May-04 9:31 
GeneralAbout Save DC Pin
AlvinYoung28-Mar-04 15:55
memberAlvinYoung28-Mar-04 15:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 30 Jan 2002
Article Copyright 2002 by NormDroid
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid