(原创)在指定区域显示图片

int ShowPIC(CDC *pDC, CString strPath, CRect rect, int ShowType)
{
    CString lastchar;
    lastchar = "";
    lastchar = strPath.Right(3);
    if(lastchar == "ico" || lastchar == "ICO")
    {
        HICON myicon;
        myicon = (HICON)LoadImage(NULL, strPath,
            IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_VGACOLOR);
        DrawIconEx(pDC->GetSafeHdc(),rect.left,rect.top,myicon,rect.Width(),rect.Height(),0,NULL,DI_NORMAL);        
        DestroyIcon(myicon);
        m_PolyAddPara.imageHeight = 32;
        m_PolyAddPara.imageWidth = 32;
    }
    else if(lastchar == "exe" || lastchar == "EXE")
    {
        HICON myicon;
        myicon = ::ExtractIcon(AfxGetInstanceHandle(),strPath,0);
        DrawIconEx(pDC->GetSafeHdc(),rect.left,rect.top,myicon,rect.Width(),rect.Height(),0,NULL,DI_NORMAL);        
        DestroyIcon(myicon);
        m_PolyAddPara.imageHeight = 32;
        m_PolyAddPara.imageWidth = 32;
    }
    else//显示BMP JPG GIF等格式的图片
    {                
        IStream *pStm = NULL;//2017.08.18    
        CFileStatus fstatus;    
        CFile file;      
        LONG cb;      
        HGLOBAL hGlobal = NULL;//2017.08.18
        //打开文件并检测文件的有效性    
        //if (!file.Open(strPath,CFile::modeRead))
        //    return -4;
            
    //    if(!file.GetStatus(strPath,fstatus))
        //    return -5;
            
        //if((cb = fstatus.m_size) == -1)     
        //    return -6;
        if (file.Open(strPath,CFile::modeRead)&&        
            file.GetStatus(strPath,fstatus)&&         
            ((cb = fstatus.m_size) != -1))         
        {          
            hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);         
            LPVOID pvData = NULL;          
            if (hGlobal != NULL)              
            {              
                pvData = GlobalLock(hGlobal);            
                if (pvData != NULL)                  
                {                  
                    file.ReadHuge(pvData, cb);                  
                    GlobalUnlock(hGlobal);                  
                    CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);                  
                }             
            }         
        }    
        else        
        {
            if(file)
                file.Close();
            return -1;        
        }
        //打开文件结束        
        //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,    
        //要显示动画GIF请使用ACTIVE控件。
        if(hGlobal == NULL || pStm == NULL)
        {
            if(hGlobal != NULL)
            {
                //释放内存
                GlobalUnlock(hGlobal);                  
                GlobalFree(hGlobal);  
            }
            if(pStm != NULL)
            {
                pStm->Release();                
            }
            file.Close();
            return -10;
        }
        
        IPicture *pPic = NULL;//F20170818     
        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;          
            //得到图片的高度与宽度        
            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);      
            
            m_PolyAddPara.imageHeight = (long)fY;
            m_PolyAddPara.imageWidth = (long)fX;
            
            long nwidth,nheight,nleft,ntop;
            int Rx,Ry;
            Rx = rect.Width();
            Ry = rect.Height();
            nheight = Ry;
            nwidth = Rx;
            nleft = rect.left;
            ntop = rect.top;        
            //检测显示模式
            //是否拉伸充满区域显示
            if(ShowType == 1)
            {
                nheight = Ry;
                nwidth = Rx;
                nleft = rect.left;
                ntop = rect.top;    
            }
            //是否居中且按正常比例显示
            else if(ShowType == 2)
            {            
                if(fX < Rx)        
                    nwidth = (long)fX;
                if(fY < Ry)        
                    nheight = (long)fY;
                
                if((fY > Ry) || (fX > Rx))
                {
                    double temp,temp2;            
                    temp = Ry / fY;
                    temp2 = Rx / fX;
                    if(temp < temp2)
                    {
                        nwidth = (long)(fX * temp);
                        nleft = (Rx-nwidth)/2 + rect.left;                
                    }
                    else if(temp >= temp2)
                    {
                        nheight = (long)(fY * temp2);
                        ntop = (Ry - nheight)/2 + rect.top;                
                    }            
                }
            }                
            //用 Render函数显示图片        
            if(FAILED(pPic->Render(*pDC,nleft,ntop,nwidth,nheight,0,
                hmHeight,hmWidth,-hmHeight,NULL)))              
            {    
                //释放内存
                GlobalUnlock(hGlobal);                  
                GlobalFree(hGlobal);  
                pPic->Release();
                pStm->Release();
                file.Close();
                return -2;            
            }    
            
            pPic->Release();  //2008.01.31
            pStm->Release();
            file.Close();
        }      
        else          
        {        
            //释放内存
            GlobalUnlock(hGlobal);                  
            GlobalFree(hGlobal);  
            pStm->Release();
            file.Close();
            return -3;          
        }    
        //释放内存
        GlobalUnlock(hGlobal);                  
        GlobalFree(hGlobal);  
    }    
    return 1;
}


你可能感兴趣的:((原创)在指定区域显示图片)