打开jpg格式的图片

作者在 2008-04-20 12:54:32 发布以下内容

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句柄

这个是打开jpg如果保存你存成jpg的不就行了,其实文件在操作中都是转成HBITMAP的

未验证 | 阅读 4045 次
文章评论,共0条
游客请输入验证码
浏览1970469次