就是运行程序后,选“文件”->“打开”,然后选择路径导入一幅图片,而不是在编写时导入一个固定位置的图片

解决方案 »

  1.   

    BOOLShowPIC(CDC *pDC, CString strPath, CRect rect,int ShowType)
    {
    CPen newpen(PS_DOT,1,RGB(255,255,255));
    CPen *pOldPen=pDC->SelectObject(&newpen);
    pDC->Rectangle(rect);
    pDC->SelectObject(pOldPen);

    //显示BMP JPG GIF等格式的图片
        IStream *pStm;  
        CFileStatus fstatus;
        CFile file;  
        LONG cb;  

    HGLOBAL hGlobal;
        //打开文件并检测文件的有效性
        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
        {
            return false;
        } 
    //打开文件结束
        

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

        IPicture *pPic; 
        //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;  
            //得到图片的高度与宽度
            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);  

    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,rect.left,rect.top ,rect.Width(),rect.Height(),0,
    if(FAILED(pPic->Render(*pDC,nleft,ntop,nwidth,nheight,0,
    hmHeight,hmWidth,-hmHeight,NULL)))  
            {
                pPic->Release();
                return false;
            }
               pPic->Release();  
        }  

        else  
        {
            return false;  
        }
    //释放内存
        GlobalUnlock(hGlobal);  
    GlobalFree(hGlobal);      return true;}