/*static */ bool display_t::save_bitmap(const char *szFileName, HBITMAP bmp, HPALETTE pal /*= NULL*/) { START_FUNCTION_BOOL(); PICTDESC pdPictDesc; pdPictDesc.cbSizeofstruct = sizeof(PICTDESC); pdPictDesc.picType = PICTYPE_BITMAP; pdPictDesc.bmp.hbitmap = bmp; pdPictDesc.bmp.hpal = pal; LPPICTURE plPicture = NULL; HRESULT hResult = OleCreatePictureIndirect(&pdPictDesc, IID_IPicture, false, reinterpret_cast<void**>(&plPicture)); if (!SUCCEEDED(hResult)) { break; } LPSTREAM lpStream = NULL; hResult = CreateStreamOnHGlobal(0, true, &lpStream); if (!SUCCEEDED(hResult)) { plPicture->Release(); return false; } LONG lBytesStreamed = 0; hResult = plPicture->SaveAsFile(lpStream, true, &lBytesStreamed); HANDLE hFile = CreateFileA(szFileName, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (!SUCCEEDED(hResult) || !hFile) { lpStream->Release(); plPicture->Release(); break; } HGLOBAL hgMemory = 0; GetHGlobalFromStream(lpStream, &hgMemory); LPVOID lpData = GlobalLock(hgMemory); DWORD dwBytesWritten = 0; bool bResult = !!WriteFile(hFile, lpData, lBytesStreamed, &dwBytesWritten, 0); bResult &= (dwBytesWritten == static_cast<DWORD>(lBytesStreamed)); GlobalUnlock(hgMemory); CloseHandle(hFile); lpStream->Release(); plPicture->Release(); END_FUNCTION_BOOL(); }
///////////////////////////////////////////////////////////////////////////////////////// // Utility to save a bitmap to file for later examination. Used for debugging only. // Based on an article by "gelbert" on www.experts-exchange.com at // http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20193761.html // // This version is a member function and instead of taking an HBITMAP as the 2nd // parameter, it take a reference to a CBitmap. Otherwise, it is identical. ///////////////////////////////////////////////////////////////////////////////////////// BOOL CSliderCtrlEx::SaveBitmap(LPCSTR lpFileName, CBitmap &hBitmap, HPALETTE hPal) { BOOL bResult = FALSE; PICTDESC stPictDesc; stPictDesc.cbSizeofstruct = sizeof(PICTDESC); stPictDesc.picType = PICTYPE_BITMAP; stPictDesc.bmp.hbitmap = hBitmap; stPictDesc.bmp.hpal = hPal; LPPICTURE pPicture; HRESULT hr = OleCreatePictureIndirect( &stPictDesc, IID_IPicture, FALSE, reinterpret_cast<void**>(&pPicture) ); if ( SUCCEEDED(hr) ) { LPSTREAM pStream; hr = CreateStreamOnHGlobal( NULL, TRUE, &pStream ); if ( SUCCEEDED(hr) ) { long lBytesStreamed = 0; hr = pPicture->SaveAsFile( pStream, TRUE, &lBytesStreamed ); if ( SUCCEEDED(hr) ) { HANDLE hFile = CreateFile(_T(lpFileName), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( hFile ) { HGLOBAL hMem = NULL; GetHGlobalFromStream( pStream, &hMem ); LPVOID lpData = GlobalLock( hMem ); DWORD dwBytesWritten; bResult = WriteFile( hFile, lpData, lBytesStreamed, &dwBytesWritten, NULL ); bResult &= ( dwBytesWritten == (DWORD)lBytesStreamed ); // clean up GlobalUnlock(hMem); CloseHandle(hFile); } } // clean up pStream->Release(); } // clean up pPicture->Release(); } return bResult; }