如何用IImage组件来播放GIF动画,jpg,png

来源:互联网 发布:淘宝的ipad为什么便宜 编辑:程序博客网 时间:2024/06/05 19:09

如何用IImage组件来播放GIF动画

GIF, IImage, 动画, 组件, 播放转自http://www.devdiv.net/bbs/viewthread.php?tid=9334

如何用IImage组件来播放GIF动画

最近小弟在网上收集资料发现IImage这个com组件相当的强大,有好多地方需要学习和分析的

哎~路还很长...

下面是播放gif动画的过程

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;
}
___________________________________________________
            IImagingFactory *pImgFactory = NULL;
            IImage *pImage = NULL;
            ImageInfo *info = NULL;
            IStream*  pStream = CreateStreamByFileName(L"//Windows//OemAnimationstartup.gif");
            IImageDecoder* pDecoder = NULL;
            UINT count = 0;
            UINT size = 0;
            ImageInfo ii = {0};
            PropertyItem pi = {0};
            GUID guid;
            UINT i = 0;
            IBitmapImage *bmpimg = NULL;
            IImageSink   *sink = NULL;
            LARGE_INTEGER dlibMove = { 0, 0 };
            IBasicBitmapOps *oper = NULL;

            hdc = BeginPaint(hwnd, &ps);

            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);
                    pDecoder->GetPropertyItem(PropertyTagFrameDelay, size, &pi);

                    pDecoder->GetImageInfo(&ii);
                    pDecoder->GetFrameDimensionsCount(&count);
                    pDecoder->GetFrameDimensionsList(&guid, count);
                    pDecoder->GetFrameCount(&guid, &count);

                    while(i < count)
                    {
                        pDecoder->SelectActiveFrame(&guid,i);
                        pImgFactory->CreateNewBitmap(ii.Width, ii.Height, PixelFormatDontCare, &bmpimg);
                        bmpimg->QueryInterface(IID_IImageSink, (void**)&sink);
                        HRESULT hr = pDecoder->BeginDecode(sink, NULL);
                        pDecoder->Decode();

                        bmpimg->QueryInterface(IID_IBasicBitmapOps, (void **) &oper);

                        oper->Rotate(180.0, InterpolationHintDefault, &bmpimg);//这个是将图像翻转,测试用的,不需要可以去掉

                        bmpimg->QueryInterface(IID_IImage, (void**)&pImage);
                        pImage->Draw(hdc, &winRect, NULL);
                        //Sleep(((long*)pi.value)[i]*10);
                        Sleep((10);
                        pDecoder->EndDecode(hr);
                        ++i;
                    }
                }
            }

            pImgFactory->Release();
            pImage->Release();
            pDecoder->Release();
            bmpimg->Release();
            sink->Release();
            oper->Release();
原创粉丝点击