码迷,mamicode.com
首页 > 编程语言 > 详细

vc++显示一张JPG图片

时间:2015-11-25 13:17:41      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

bool CshowjpgDlg::ShowJpgGif(CDC* pDC,CString strPath, int x, int y)
{
    IStream *pStm; 
    CFileStatus fstatus; 
    CFile file; 
    LONG cb;

    if (file.Open(strPath,CFile::modeRead)&&file.GetStatus(strPath,fstatus)&&((cb = fstatus.m_size) != -1)) 
    { 
        HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb); 
        LPVOID pvData = NULL; 

        if (hGlobal != NULL) 
        { 
            pvData = GlobalLock(hGlobal);
            if (pvData != NULL) 
            { 
                file.Read(pvData, cb); 
                GlobalUnlock(hGlobal); 
                CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); 
            }
        }
    }
    else
    {
        return false;
    }

    //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
    //要显示动画GIF请使用ACTIVE控//件。
    IPicture *pPic = nullptr;

    //load image from file stream
    if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
    {
        OLE_XSIZE_HIMETRIC hmWidth; 
        OLE_YSIZE_HIMETRIC hmHeight; 

        pPic->get_Width(&hmWidth); 
        pPic->get_Height(&hmHeight); 
        double fX,fY;

        //get image height and width
        fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0); 
        fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0); 

        

        //use render function display image
        if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL))) 
        {
            pPic->Release();
            return false;
        }
        pPic->Release();
    } 
    else 
        return false; 
    return true;
}

2.调用
 CDC* pDC = GetDlgItem(IDC_PICTRUE11)->GetDC();
 ShowJpgGif(pDC,_T("d.jpg"),0,0);

vc++显示一张JPG图片

标签:

原文地址:http://www.cnblogs.com/ouyangyuxi/p/4994085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!