Display GIF animation with GDI +

Source: Internet
Author: User


A multi-page image contains multiple graphic pages. Each page can be seen as an image frame. These image frames form an animation through continuous display. For exampleGIFFile.

GDI + Of Image Object provides direct GIF , TIF File format. Call Image Object member functions Getframedimensionscount You can get Image Object Dimension Number. Each Dimension Through Guid Mark. Function Getframedimensionslist Returns all Dimension Of Guid Value. First Guid Value stored in function parameters Pdimensionsids Array Index 0 . Getframecount You can obtain Dimension Number Frame .Simple ExampleCode:
Image * image = new image (L "multiframe.gif ");
Uint COUNT = 0;
Count = image-> getframedimensionscount ();
Guid * pdimensionids = (guid *) New guid [count];
Image-> getframedimensionslist (pdimensionids, count );
Wchar strguid [39];
Stringfromguid2 (pdimensionids [0], strguid, 39 );
Uint framecount = image-> getframecount (& pdimensionids [0]);

Delete [] pdimensionids;
Not all GIF Files contain multiple frames, so we are GIF You can use the above Code Framecount To determine this GIF Whether the file has multiple frames.

After you confirm that there are multiple frames of the image, you need to obtain the display interval of each frame of the image. GDI + Of Image Object provides Getpropertyitem Obtains the attributes of an image. Getpropertyitem The function requires the user to pass the returned data buffer and size. Therefore, use Getpropertyitemsize Obtain the required buffer size, allocate space, and then obtain the attribute data.

// Propertytagframedelay Yes GDI + Pre-defined Gig Attribute ID Value, indicating the delay time of the tag frame data.

Int size = getpropertysize (propertytagframedelay );
Propertyitem * pitem = NULL;
Pitem = (propertyitem *) malloc (size );
Image-> getpropertyitem (propertytagframedelay, size, pitem );
In this way Propertytagframedelay Property-related data is retrieved Pitem . Then Pitem In the access structure Value . The interval between two frames is not necessarily the same. Therefore, you need to obtain the index value of the currently displayed frame image. Last call Image Object Drawimage The function draws an image of each frame. The simple code is as follows:
IntFcount = 0;
// Guid Value display GIF Is Framedimensiontime , Display TIF Is Framedimensionpage
GuidGuid = framedimensiontime;
While (thue)
Graphics GH (HDC); // HDC Is an external drawing DC
Gh. drawimage (image, 0, 0, image-> getwidth (), image-> getheight ());
// Reset the current active data frame
Image-> selectactiveframe (& guid, fcount ++ );
If (fcount = framecount) // framecount Yes Getframecount Return Value
Fcount = 0;// If the last frame of data starts again
// Calculate the delay time of this frame.
Long lpause = (long *) pitem-> value) [fcount] * 10;
Sleep (lpause );// This is simple to use. Sleep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.