Gdi+ 展示gif

什么是Gif ?

gif可以理解成一组图片的集合。每一帧代表一幅图。gif文件里面包含了帧的数量以及每帧图的时间间隔。

如何加载gif ?

原理:解析gif文件里面的帧数量以及每帧图的时间间隔,定时绘画每帧图即可。

  • 解析gif 获取帧数量和每帧时间间隔
	g_pgif = new Gdiplus::Image(L".//00.gif");
	int nDimensionsCount = g_pgif->GetFrameDimensionsCount();
	std::vector<GUID> vecDimensions(nDimensionsCount);
    g_pgif->GetFrameDimensionsList(&vecDimensions[0], vecDimensions.size());
    g_nframecount = g_pgif->GetFrameCount(&vecDimensions[0]);

	int Delaysize = g_pgif->GetPropertyItemSize(PropertyTagFrameDelay);
    if (Delaysize > 0)
    {
		std::vector<char> vecProp(Delaysize);
		PropertyItem* pPro = (PropertyItem*)&vecProp[0];
		g_pgif->GetPropertyItem(PropertyTagFrameDelay, vecProp.size(), pPro);
		int* pval = (int*)pPro->value;
		g_vec_delay_time = std::vector<int>(pval, pval + g_nframecount);
    }
  • 根据当前帧计算时长绘画窗口
 	g_now_frame = ++g_now_frame% g_nframecount;
 	//刷新窗口 ::InvalidateRect(hWnd, NULL, true);
    ::PostMessage(hWnd, UM_PAINT, 0, 0);
    if(!g_vec_delay_time.empty())
    	::SetTimer(hWnd, GIF_TIMERID, g_vec_delay_time[g_now_frame]*10, NULL);
  • 绘画窗口
	GUID guid = FrameDimensionTime;
    g_pgif->SelectActiveFrame(&guid, g_now_frame);
	g.DrawImage(g_pgif, 100, 100, 200, 200);

你可能感兴趣的:(C++,随笔,Gdi+,gif)