YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   Home Products | Purchase Support | Downloads  
View in English
View in Japanese
View in
참고
View in Français
View in Italiano
View in 中文(繁體)
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
VX++ Cross-Platform C/C++
Overview
Download
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

Play GIF, Load GIF, GIF Animate, with GDI+ Example

Example 1

 Introduction

In the past, there was no convenient enough way to play GIF using functions provided by Microsoft Windows but you may need a reference of 3rd libs. Well, now we have an alternative choice --- to use GDI+ .

Critical Code

Collapse | Copy Code
//Here, we load a GIF image file
void CGIFControl::Load(LPCTSTR sFileName)
{
	m_pImage = new Image(sFileName);
	
	//First of all we should get the number of frame dimensions
	//Images considered by GDI+ as:
	//frames[animation_frame_index][how_many_animation];
	UINT count = m_pImage->GetFrameDimensionsCount();

	//Now we should get the identifiers for the frame dimensions 
	m_pDimensionIDs =new GUID[count];
	m_pImage->GetFrameDimensionsList(m_pDimensionIDs, count);
	
	//For gif image , we only care about animation set#0
	WCHAR strGuid[39];
	StringFromGUID2(m_pDimensionIDs[0], strGuid, 39);
	m_FrameCount = m_pImage->GetFrameCount(&m_pDimensionIDs[0]);

	//PropertyTagFrameDelay is a pre-defined identifier 
	//to present frame-delays by GDI+
	UINT TotalBuffer = m_pImage->GetPropertyItemSize(PropertyTagFrameDelay);
	m_pItem = (PropertyItem*)malloc(TotalBuffer);
	m_pImage->GetPropertyItem(PropertyTagFrameDelay,TotalBuffer,m_pItem);
}

//To start play
void CGIFControl::Play()
{
	//Set Current Frame at #0
	m_iCurrentFrame = 0;
	GUID Guid = FrameDimensionTime;
	m_pImage->SelectActiveFrame(&Guid,m_iCurrentFrame);

	//Use Timer
	//NOTE HERE: frame-delay values should be multiply by 10
	SetTimer(1,((UINT*)m_pItem[0].value)[m_iCurrentFrame]  * 10,NULL);

	//Move to the next frame
	++ m_iCurrentFrame;
	Invalidate(FALSE);
}

//Using timer
void CGIFControl::OnTimer(UINT_PTR nIDEvent)
{
	//Because there will be a new delay value
	KillTimer(nIDEvent);

	//Change Active frame
	GUID Guid = FrameDimensionTime;
	m_pImage->SelectActiveFrame(&Guid,m_iCurrentFrame);

	//New timer
	SetTimer(1,((UINT*)m_pItem[0].value)[m_iCurrentFrame] * 10,NULL);

	//Again move to the next
	m_iCurrentFrame = (++ m_iCurrentFrame) % m_FrameCount;
	Invalidate(FALSE);
}

//Finally simply draw
g.DrawImage(m_pImage,rcClient.left,rcClient.top,rcClient.Width(),rcClient.Height());
//Will present the current frame

That's the core.

Using the Code

Simple usage:

Collapse | Copy Code
LinaBot::CommonCtrl::CGIFControl* pCtrl = new LinaBot::CommonCtrl::CGIFControl;
pCtrl->Create(rcCreate,TopWindow,c.wData.nID);
pCtrl->Load(GetSkinFileName(c.wData.BackGround));
pCtrl->SetURL(c.wData.sURL);
pCtrl->Play();

Example 2

Motive

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 (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp) 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.

Implementation

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

Collapse | Copy Code
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 SelectObject 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 tucancode.net is to determine whether or not a GIF is of the animated kind. The following code demonstrates this:

Collapse | Copy Code
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;
}

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 GetPropertyItem 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 DrawFrameGIF until the object destructs. See DrawFrameGIF below:

Collapse | Copy Code
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, m_rc.top, 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).

Conclusion

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.

 

Contact UCanCode Software

To buy the source code or learn more about with:

 

Ask any questions by MSN: ucancode@hotmail.com Yahoo: ucan_code@yahoo.com


 

Copyright ?1998-2023 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net