Today, I encountered the problem of using GDI + to load image streams. I couldn't find a lot of information on the Internet, and other frames were always blurred ,,
The online method code is as follows:
Note: pimagedata is of the byte * type and dimagelen is of the DWORD type;
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 (bytes); m_pitem = (gdiplus: propertyitem *) malloc (totalbuffer ); m_pimage-> getpropertyitem (propertytagframedelay, totalbuffer, m_pitem); m_bstatok = true ;}}
Always draw a method to solve the problem after the problem occurs. The Code is as follows:
Note: m_pstream is a class member and is released during class analysis. pimagedata is of the byte * type and dimagelen is of the DWORD type;
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; 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 (bytes); m_pitem = (gdiplus: propertyitem *) malloc (totalbuffer); m_pimage-> getpropertyitem (bytes, totalbuffer, m_pitem ); m_bstatok = true ;}}}
In this way, other frames can be drawn through the timer without image imaging.
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;}
GDI + loads GIF images (convert byte * To istream)