今天工作中遇到GDI+加载图片流的问题,网上找了很多资料都不行,绘制其他帧总是模糊,,
网上的方法代码如下:
注:pImageData是BYTE*类型 ,dImageLen是DWORD类型; HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, dImageLen); BYTE* pmem = (BYTE*)GlobalLock(m_hMem); memcpy(pmem,pImageData,dImageLen); IStream* pstm; HRESULT ht = CreateStreamOnHGlobal(m_hMem,FALSE,&pstm); if (ht != S_OK) { GlobalFree(m_hMem); return ; } m_pImage=Gdiplus::Image::FromStream(pstm); GlobalUnlock(m_hMem); pstm->Release(); GlobalFree(m_hMem); if (m_pImage) { UINT count = m_pImage->GetFrameDimensionsCount(); if (count > 0 && m_pImage->GetLastStatus() == 0) { m_pDimensionIDs = new GUID[count]; m_pImage->GetFrameDimensionsList(m_pDimensionIDs, count); WCHAR strGuid[39]; StringFromGUID2(m_pDimensionIDs[0], strGuid, 39); m_FrameCount = m_pImage->GetFrameCount(&m_pDimensionIDs[0]); UINT TotalBuffer = m_pImage->GetPropertyItemSize(PropertyTagFrameDelay); m_pItem = (Gdiplus::PropertyItem*)malloc(TotalBuffer); m_pImage->GetPropertyItem(PropertyTagFrameDelay, TotalBuffer, m_pItem); m_bStatOK = TRUE; } }
总是绘制出现问题后自己找了方法解决问题,代码如下:
注:m_pstream是类成员,在类析构的时候释放 ,,,pImageData是BYTE*类型 ,dImageLen是DWORD类型; if(CreateStreamOnHGlobal(NULL, TRUE, &m_pStream) == S_OK) { ULONG ulWritten = 0; m_pStream->Write(pImageData, dImageLen, &ulWritten); //Rewind the argument stream; LARGE_INTEGER lInt; lInt.QuadPart = 0; m_pStream->Seek(lInt, STREAM_SEEK_SET, NULL); //Read the lenght of the argument stream; STATSTG statSTG; long dwResult = m_pStream->Stat(&statSTG, STATFLAG_DEFAULT); if(dwResult != S_OK) { return ; } m_pImage=Gdiplus::Image::FromStream(m_pStream); if (m_pImage) { UINT count = m_pImage->GetFrameDimensionsCount(); if (count > 0 && m_pImage->GetLastStatus() == 0) { m_pDimensionIDs = new GUID[count]; m_pImage->GetFrameDimensionsList(m_pDimensionIDs, count); WCHAR strGuid[39]; StringFromGUID2(m_pDimensionIDs[0], strGuid, 39); m_FrameCount = m_pImage->GetFrameCount(&m_pDimensionIDs[0]); UINT TotalBuffer = m_pImage->GetPropertyItemSize(PropertyTagFrameDelay); m_pItem = (Gdiplus::PropertyItem*)malloc(TotalBuffer); m_pImage->GetPropertyItem(PropertyTagFrameDelay, TotalBuffer, m_pItem); m_bStatOK = TRUE; } } }
这样通过定时器绘制其他帧就没有图片不成像问题
DrawGif(HDC hDC, const RECT& rc) { if (NULL == m_pItem) return 0; m_pImage->SelectActiveFrame(&Gdiplus::FrameDimensionTime, m_iCurrentFrame); unsigned msec = ((int*)m_pItem->value)[m_iCurrentFrame] * 10; Graphics g( hDC ); g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality); g.DrawImage( m_pImage, rc.left, rc.top, rc.right-rc.left, rc.bottom - rc.top ); return msec; }
时间: 2024-10-14 08:54:58