文档描述: | PNG、JPG、BMP和GIF是图片中比较常用的格式;本文档描述了在华禹工控P1310 WinCE5.0上播放这些图片的方法 |
文档版本: | V1.0 |
在图片文件的头部,标识了图片的类型;图片浏览软件有一个支持的图片类型的列表,通过比对格式,可确定当前图片为哪种格式。 有些图片浏览软件对后缀名有严格的要求,比如PS,它会比对后缀名和图片头文件中的内容。 |
typedef BOOL(STDAPICALLTYPE FAR fAlphaBlend)(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
static fAlphaBlend * pAlphaBlend = NULL;
HMODULE hCoredllLib = NULL;
hCoredllLib = LoadLibrary(L "coredll.dll");
pAlphaBlend = (fAlphaBlend * ) GetProcAddress(hCoredllLib, L "AlphaBlend");
void DrawTransparentPng(HWND hWnd){
LPCTSTR szFileName = L"ResidentFlash2\\transparent.png";
HRESULT hr;
HDC hdc;
IImagingFactory* pFactory;
IImage* pImage;
ImageInfo imageInfo;
RECT rc;
hr = ::CoInitializeEx(NULL,COINIT_MULTITHREADED);
if(FAILED(hr)){
::MessageBox(NULL,L"CoInitializeEx fail",L"info",MB_OK);
return;
}
hr = ::CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**)&pFactory);
if(FAILED(hr)){
::CoUninitialize();
::MessageBox(NULL,L"CoCreateInstance fail",L"info",MB_OK);
return;
}
hr = pFactory->CreateImageFromFile(szFileName,&pImage);
if(FAILED(hr)){
pFactory->Release();
::CoUninitialize();
::MessageBox(NULL,L"IImagingFactory CreateImageFromFile fail",L"info",MB_OK);
return;
}
pImage->GetImageInfo(&imageInfo);
hdc = ::GetDC(hWnd);
rc.left = 40;
rc.top =40;
rc.right = imageInfo.Width+rc.left;
rc.bottom = imageInfo.Height+rc.top;
pImage->Draw(hdc,&rc,NULL);
::ReleaseDC(hWnd,hdc);
if(pImage){
pImage->Release();
}
if(pFactory){
pFactory->Release();
}
::CoUninitialize();
}
IStream* CreateStreamByFileName(LPCTSTR strFileName)
{
HANDLE hFile = CreateFile(strFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return 0;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == (DWORD)-1)
{
CloseHandle(hFile);
return 0;
}
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
if (hGlobal == NULL)
{
CloseHandle(hFile);
return 0;
}
LPVOID pvData = GlobalLock(hGlobal);
if (pvData == NULL)
{
GlobalUnlock(hGlobal);
CloseHandle(hFile);
return 0;
}
DWORD dwBytesRead = 0;
BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
if (!bRead)
{
return 0;
}
IStream* pStream = 0;
if (FAILED(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream)))
{
return 0;
}
return pStream;
}
void PlayGifImage(LPCTSTR strFileName,HDC hdc,const RECT &rcDraw)
{
IImagingFactory *pImgFactory = NULL;
IImage *pImage = NULL;
ImageInfo *info = NULL;
IStream* pStream = CreateStreamByFileName(strFileName);
IImageDecoder* pDecoder = NULL;
UINT count = 0;
UINT size = 0;
ImageInfo ii = {0};
PropertyItem *pi = NULL;
GUID guid;
UINT i = 0;
IBitmapImage *bmpimg = NULL;
IImageSink *sink = NULL;
LARGE_INTEGER dlibMove = { 0, 0 };
if (FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
pStream->Release();
return;
}
if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)
&pImgFactory)))
{
if (SUCCEEDED(pImgFactory->CreateImageDecoder(pStream, DecoderInitFlagNone, &pDecoder)))
{
pDecoder->TerminateDecoder();
pStream->Seek( dlibMove, STREAM_SEEK_SET, NULL );
pDecoder->InitDecoder(pStream, DecoderInitFlagBuiltIn1st);
pDecoder->GetPropertyItemSize(PropertyTagFrameDelay, &size);
pi = reinterpret_cast
pDecoder->GetPropertyItem(PropertyTagFrameDelay, size, pi);
pDecoder->GetImageInfo(&ii);
pDecoder->GetFrameDimensionsCount(&count);
pDecoder->GetFrameDimensionsList(&guid, count);
pDecoder->GetFrameCount(&guid, &count);
pImgFactory->CreateNewBitmap(ii.Width, ii.Height, PixelFormatDontCare, &bmpimg);
bmpimg->QueryInterface(IID_IImageSink, (void**)&sink);
bmpimg->QueryInterface(IID_IImage, (void**)&pImage);
while(TRUE)
{
if(i >= count) {
i = 0;
break;
}
pDecoder->SelectActiveFrame(&guid,i);
HRESULT hr = pDecoder->BeginDecode(sink, NULL);
pDecoder->Decode();
pImage->Draw(hdc, &rcDraw, NULL);
DWORD dwSleep = (reinterpret_cast
Sleep(dwSleep);
pDecoder->EndDecode(hr);
++i;
}
if(bmpimg){
bmpimg->Release();
}
if(sink){
sink->Release();
}
if(pImage){
pImage->Release();
}
}
}
if(pi){
free(pi);
}
if(pDecoder){
pDecoder->Release();
}
if(pStream){
pStream->Release();
}
if(pImgFactory){
pImgFactory->Release();
}
CoUninitialize();
}
void DrawGif(HWND hWnd){
LPCTSTR szFileName = L"ResidentFlash2\\poweroff.gif";
RECT rc;
HDC hdc = ::GetDC(hWnd);
::GetClientRect(hWnd,&rc);
PlayGifImage(szFileName,hdc,rc);
::ReleaseDC(hWnd,hdc);
}