HBITMAP JpgToBmp(LPCTSTR szFile, HBITMAP hRet)
{
LPPICTURE gpPicture;
HANDLE
hFile = ::CreateFile(szFile,
GENERIC_READ,0,NULL,
OPEN_EXISTING, 0,NULL);
if(INVALID_HANDLE_VALUE
==
hFile)
return
false;
DWORD dwFileSize = ::GetFileSize(hFile,NULL);
ASSERT(-1!=dwFileSize);
LPVOID pvData = NULL;
HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE,
dwFileSize);
ASSERT(NULL!=hGlobal);
pvData = ::GlobalLock(hGlobal);
ASSERT(NULL!=pvData);
DWORD dwBytesRead = 0;
BOOL
bRead
=
::ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,NULL);
ASSERT(FALSE!= bRead);
::GlobalUnlock(hGlobal);
::CloseHandle(hFile);
LPSTREAM pstm =
NULL;
HRESULT hr = ::CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
ASSERT(SUCCEEDED(hr)&&pstm);
hr=::OleLoadPicture(pstm,dwFileSize,FALSE,IID_IPicture,
(LPVOID*)&gpPicture);
ASSERT(SUCCEEDED(hr)&&gpPicture);
pstm->Release();
OLE_HANDLE picHandle;
gpPicture->get_Handle(&picHandle);
HDC hdc =
::CreateCompatibleDC(NULL);
ASSERT(hdc);
::SelectObject(hdc,(HGDIOBJ)picHandle);
hRet = (HBITMAP)::GetCurrentObject(hdc,OBJ_BITMAP);
DeleteDC(hdc);
return
hRet;
}
用法
HBITMAP hbitmap;
hbitmap = JpgToBmp("你的jpg文件路径",hbitmap);
返回一个HBITMAP句柄