Gdiplus 读取并显示 Gif

Gdiplus提供的Bitmap支持读取并显示GIF文件.

但是对于部分GIF图片可能会出现帧间间隔为0的情况,为此,在此情况下默认时间间隔为100ms, 不知道是不是我没有找对,有建议欢迎反馈! (测试在GIMP中同样会出现帧间间隔为0,我想应是有个默认值的吧,100ms效果还可以接受,暂且借用)

#define UM_TIMER_GIFNEXTFRAME (WM_USER+0x01) //message for timer int iphotoframecurrent = 0; int iphotoframeindex; GUID* pphotoframeDimensionIDs = NULL; PropertyItem* pphotoframeItem = NULL; void GifInit(Bitmap* pSrc) { iphotoframeindex = pSrc->GetFrameDimensionsCount(); pphotoframeDimensionIDs = (GUID*)new GUID[iphotoframeindex]; pSrc->GetFrameDimensionsList(pphotoframeDimensionIDs, iphotoframeindex); WCHAR strGuid[64] = {0}; StringFromGUID2(pphotoframeDimensionIDs[0], strGuid, 64); int iphotoframecount=pSrc->GetFrameCount(&pphotoframeDimensionIDs[0]); if (iphotoframecount==1) //not dynamic GIF, only one frame { return; } UINT TotalBuffer = pSrc->GetPropertyItemSize(PropertyTagFrameDelay); pphotoframeItem = (PropertyItem*)malloc(TotalBuffer); pSrc->GetPropertyItem(PropertyTagFrameDelay,TotalBuffer,pphotoframeItem); iphotoframecurrent = 0; GUID Guid = FrameDimensionTime; pSrc->SelectActiveFrame(&Guid,iphotoframecurrent); // Get current frame int delay = ((UINT*)pphotoframeItem[0].value)[iphotoframecurrent]; if (0==delay) // No delay time, use default 100ms { delay = 100; } SetTimer(UM_TIMER_GIFNEXTFRAME, delay, NULL); ++ iphotoframecurrent; g_bGIFStoped = FALSE; } void OnTimer(UINT_PTR nIDEvent) { if (UM_TIMER_GIFNEXTFRAME == nIDEvent) { KillTimer(nIDEvent); if (iphotoframecount==1) { return; } GUID Guid = FrameDimensionTime; pSrc->SelectActiveFrame(&Guid,iphotoframecurrent); int delay = ((UINT*)pphotoframeItem[0].value)[iphotoframecurrent]*10; // i also want to know why mul 10 here?? if(0==delay) delay = 100; SetTimer(UM_TIMER_GIFNEXTFRAME, delay, NULL); iphotoframecurrent = (++iphotoframecurrent) % iphotoframecount; } } free(pphotoframeItem); // Free after used! delete [] pphotoframeDimensio

 

具体代码见上。

主要思路是读取GIF文件, 通过pSrc->GetFrameCount()获取GIF的帧数, 通过 pSrc->SelectActiveFrame()来设置当前图片为GIF中的第X帧, 最后通过sleep() 和 Timer() 来实现时间延迟和当前帧更新.

 

上述代码中:

Bitmap* pSrc = ::new Bitmap(filename);

同样最后需要

::delete pSrc;

 

你可能感兴趣的:(timer,测试,user,null,delete,delay)