win32显示gif图片

最近在写win32的发送图片程序,由于要求适配gif格式,不得不强迫自己写了这么一个程序,主要参考了VC++ win32 sdk 显示 gif 图片,不过里面的每一帧的显示时间时间设成了固定值,我在代码中写了一个函数获取gif的间隔时间进行了修改

我把函数写到了一个cpp和一个h文件中,大家使用的时候调用OpenGifThread函数就可了

ShowGif.h的内容

#include <windows.h>  
#include <ocidl.h>  
#include <olectl.h>  
#include <vector>  
#include <math.h>  
#include <process.h>  
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib,"GdiPlus.lib")
typedef struct gifImage{
	WORD logX;
	WORD logY;
	WORD width;
	WORD height;
	struct flag{
		BYTE d : 3;
		BYTE c : 1;
		BYTE b : 3;
		BYTE a : 1;
	}Flag;
}GifImage, *PGifImage;

struct ArgInfo{
	HWND       hwnd;
	LPCTSTR   szImagePath;
};
a
UINT WINAPI DrawGif(LPVOID pParam);
UINT OpenGifThread(HWND hWnd, LPCTSTR szImagePath);
int GetFramePause(const ArgInfo* arg);
bool IsGif(LPCTSTR szImagePath);
void GetGifFrame(void *pImageMemory, BYTE* lpBy, BYTE** pByte, DWORD * nu, DWORD& dwReadedSize, int& num, DWORD& firstLocation);
void ShowFrame(HWND& hWnd, HGLOBAL& hImageMemory, BYTE* lpBy, BYTE** pByte, DWORD * nu, DWORD& dwReadedSize, int& num, DWORD& firstLocation, int& lPause);


ShowGif.cpp的内容

#include "ShowGif"


HANDLE m_hThread = 0;
ArgInfo Info;
const int MaxFrameNumber = 1000;

UINT WINAPI DrawGif(LPVOID pParam)
{
	ArgInfo* arg = (ArgInfo*)pParam;
	HWND hWnd = arg->hwnd;
	LPCTSTR szImagePath = arg->szImagePath;
	//从指定的路径szImagePath中读取文件句柄  
	HANDLE hFile = CreateFile(szImagePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	//获得图片文件的大小,用来分配全局内存  
	DWORD dwFileSize = GetFileSize(hFile, NULL);
	//给图片分配全局内存  
	HGLOBAL hImageMemory = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
	void *pImageMemory = GlobalLock(hImageMemory); //锁定内存  
	DWORD dwReadedSize; //保存实际读取的文件大小  
	ReadFile(hFile, pImageMemory, dwFileSize, &dwReadedSize, NULL); //读取图片到全局内存当中  
	GlobalUnlock(hImageMemory); //解锁内存  
	CloseHandle(hFile); //关闭文件句柄  
	

	BYTE* lpBy = (BYTE*)pImageMemory;   //获得图象的首地址  
	BYTE* pByte[10000];                    //用来储存gif每幅图象的地址  
	DWORD nu[10000];                   //用来储存每幅图象的大小 
	int num = 0;                    //用来计算有几副图象  
	DWORD firstLocation = 0;        //第一副图象的位置,用来替换 

	/*将gif分割为多个图片*/
	GetGifFrame(pImageMemory, lpBy, pByte, nu, dwReadedSize, num, firstLocation);

	/*获取gif的帧的时间*/
	int lPause = GetFramePause(arg);

	/*显示图片*/
	ShowFrame(hWnd, hImageMemory, lpBy, pByte, nu, dwReadedSize, num, firstLocation, lPause);

	return 0;
}

int GetFramePause(const ArgInfo* arg)//获取首个帧的间隔 在这里我们假设每个帧的间隔一样
{
	/*获取gif的每一帧的时间*/

	ULONG_PTR gdiplusToken;
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	Image* image = Image::FromFile(arg->szImagePath);

	//获得有多少个维度,对于gif就一个维度  
	UINT count = image->GetFrameDimensionsCount();

	//获得各帧之间的时间间隔  
	//先获得有多少个时间间隔,PropertyTagFrameDelay是GDI+中预定义的一个GIG属性ID值,表示标签帧数据的延迟时间  
	UINT   FrameDelayNums = image->GetPropertyItemSize(PropertyTagFrameDelay);
	PropertyItem *  lpPropertyItem = new  PropertyItem[FrameDelayNums];
	image->GetPropertyItem(PropertyTagFrameDelay, FrameDelayNums, lpPropertyItem);
	int lPause = ((int*)lpPropertyItem->value)[0] * 10; //实际上每一个帧的间隔都在数组中,在这里我们仅仅需要一个值
	return lPause;
}

UINT OpenGifThread(HWND hWnd, LPCTSTR szImagePath)
{

	Info.hwnd = hWnd;
	Info.szImagePath = szImagePath;

	if (!IsGif(szImagePath)) //不是gif格式
		return 0;
	unsigned int nDummy;
	m_hThread = (HANDLE)_beginthreadex(NULL, 0, DrawGif, &Info, 0, &nDummy);

	if (!m_hThread)
	{
		//ATLTRACE(L"Draw: Couldn't start a GIF animation thread\n");  
		return FALSE;
	}
	else
	{
		ResumeThread(m_hThread);
	}

	return 1;
}

bool IsGif(LPCTSTR szImagePath)
{
	int length = _tcslen(szImagePath);
	if (szImagePath[length - 3] != 'g')
		return false;
	if (szImagePath[length - 2] != 'i')
		return false;
	if (szImagePath[length - 1] != 'f')
		return false;
	return true;

}

void GetGifFrame(void *pImageMemory, BYTE* lpBy, BYTE** pByte, DWORD * nu, DWORD& dwReadedSize,
	int& num ,DWORD& firstLocation )
{



	 
	for (DWORD j = 0; j < dwReadedSize; j++)
	{
		if (lpBy[j] == 0x2c) //图象开头  
		{
			if (lpBy[j - 1] == 0x00)             //确认是否图象开头  
			{
				if (num == 0)
				{
					firstLocation = j;     //得到第一副图象位置  
				}
				PGifImage nowImage = (PGifImage)&lpBy[j + 1];
				if (nowImage->Flag.a == 0)    //a为0时指图象不存在局部调色板  
				{
					DWORD number = 1 + sizeof(GifImage);
					while (lpBy[j + number] != 0)
					{
						number = number + (DWORD)lpBy[j + number] + 1;
					}                        //算得图象大小  
					number++;                //把最后一个0x00加上  
					pByte[num] = new BYTE[number];
					for (DWORD n = 0; n < number; n++)
					{
						*(BYTE*)(pByte[num] + n) = lpBy[j + n];
					}                        //将图象储存起来。  
					nu[num] = number;
					j = j + number - 1;            //跳过图象  
					num++;
				}
				else
				{
					//当a为1时需要加上局部调色板的大小,其他与a为0时一样  
					int number = 1 + sizeof(GifImage)+3 * (int)floor(pow(2.0f, nowImage->Flag.d));
					while (lpBy[j + number] != 0)
					{
						number = number + (DWORD)lpBy[j + number] + 1;
					}                        //算得图象大小  

					number++;                //把最后一个0x00加上  

					pByte[num] = new BYTE[number];

					for (DWORD n = 0; n < (DWORD)number; n++)
					{
						*(BYTE*)(pByte[num] + n) = lpBy[j + n];
					}                        //将图象储存起来。  
					nu[num] = number;
					j = j + number - 1;            //跳过图象  
					num++;
				}
			}
		}
	}

}

void ShowFrame(HWND& hWnd, HGLOBAL& hImageMemory, BYTE* lpBy, BYTE** pByte, DWORD * nu, 
	DWORD& dwReadedSize, int& num, DWORD& firstLocation, int& lPause)
{
	HDC hDC = GetDC(hWnd);
	HDC hMemDC = CreateCompatibleDC(hDC);
	RECT rt;
	GetClientRect(hWnd, &rt);
	int width = rt.right - rt.left;
	int height = rt.bottom - rt.top;
	HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, width, height); // 创建兼容位图  
	HBITMAP hOldMem = (HBITMAP)SelectObject(hMemDC, hMemBmp);


	Sleep(10);
	while (true) {
		for (int m = 0; m < num; m++) {

			DWORD DDD;
			//修改页面的保护属性,以进行写操作  
			VirtualProtect(lpBy, dwReadedSize, PAGE_READWRITE, &DDD);

			for (DWORD n = 0; n < nu[m]; n++)
			{
				lpBy[firstLocation + n] = *(BYTE*)(pByte[m] + n);
			}
			VirtualProtect(lpBy, dwReadedSize, DDD, NULL);

			//因为IPicture必须把图象存成流的形式才能工作,所以有下面一段函数  
			IStream *pIStream;//创建一个IStream接口指针,用来保存图片流  
			IPicture *pIPicture;//创建一个IPicture接口指针,表示图片对象  
			CreateStreamOnHGlobal(hImageMemory, false, &pIStream); //用全局内存初使化IStream接口指针  
			OleLoadPicture(pIStream, 0, false, IID_IPicture, (LPVOID*)&(pIPicture));//用OleLoadPicture获得IPicture接口指针  
			//得到IPicture COM接口对象后,你就可以进行获得图片信息、显示图片等操作  
			OLE_XSIZE_HIMETRIC hmWidth;
			OLE_YSIZE_HIMETRIC hmHeight;
			pIPicture->get_Width(&hmWidth); //用接口方法获得图片的宽和高  
			pIPicture->get_Height(&hmHeight);
			pIPicture->Render(hMemDC, 0, 0, width, height, 0, hmHeight, hmWidth, -hmHeight, NULL); //在指定的DC上绘出图片  
			//GlobalFree(hImageMemory); //释放全局内存  
			pIStream->Release(); //释放pIStream  
			pIPicture->Release(); //释放pIPicture  

			BitBlt(hDC, 0, 0, width, height, hMemDC, 0, 0, SRCCOPY);
			Sleep(lPause);                  //停止一段时间。  
		}
	}
	GlobalFree(hImageMemory); //释放全局内存  
	CloseHandle(m_hThread);

	SelectObject(hMemDC, hOldMem);
	DeleteDC(hMemDC);
	DeleteObject(hMemBmp);
}



win32显示gif图片

上一篇:使用WindowManager添加自定义视图


下一篇:C# tostring 格式化输出