VC/MFC加载图片(JPG、JPEG、BMP)

//加载jpg图片
int m_TotCount;
CImageList m_ImageList;
CListCtrl m_List;
HIMAGELIST hScreens = ImageList_Create(THUMWIDTH, THUMHEIGHT, ILC_COLOR32, 0, 1);
m_ImageList.Attach(hScreens);

m_list.SetImageList(&m_ImageList, LVSIL_NORMAL);

for (int i = 0; i<4; i++ )
{
    CString ss;
    ss.Format("%d",i);
    HBITMAP bitm=LoadPicture( GetExeAbsolutePath()+ss+".jpg" );
    if (bitm!=NULL)
    {
        m_TotCount=m_list.GetItemCount();
        CBitmap*    pImage = NULL;   
        pImage = new CBitmap();       
        pImage->Attach(bitm);
        int imgP=m_ImageList.Add(pImage,RGB(0,0,0));
        m_list.InsertItem(m_TotCount,ss+".jpg",imgP);       
        m_list.SetItemText(m_TotCount, 1, ss+".jpg");
    }   
}

//加载Jpg、Bmp图片,这个函数是关键代码!!!!!!!
HBITMAP CAddbmpDlg::LoadPicture(CString mFile)
{
    CString pFSize;
    WCHAR wpath[MAX_PATH];
    MultiByteToWideChar(CP_ACP, 0, mFile, -1, wpath, MAX_PATH);
   
    IPicture* pPic;
    OleLoadPicturePath(wpath, NULL, NULL, NULL, IID_IPicture,(LPVOID*)&pPic);
   
    if (pPic==NULL) return NULL;
    HBITMAP hPic = NULL;
    pPic->get_Handle((UINT*)&hPic);
   
    long nWidth=THUMWIDTH;
    long nHeight=THUMHEIGHT;
   
    long mWid,mHei;
    pPic->get_Height(&mHei);
    pPic->get_Width(&mWid);
   
    HBITMAP hPicRet = (HBITMAP)CopyImage(hPic, IMAGE_BITMAP, nWidth, nHeight , LR_COPYDELETEORG);   
   
   
    HBRUSH hBrushBorder=::CreateSolidBrush(RGB(192, 192, 192));
    HBRUSH hBrushBk=::CreateSolidBrush(RGB(255, 255, 255));
   
   
    RECT rcBorder;
    rcBorder.left=rcBorder.top=0;
    rcBorder.right=THUMWIDTH;
    rcBorder.bottom=THUMHEIGHT;
   
    const float fRatio=(float)THUMHEIGHT/THUMWIDTH;
   
    int XDest, YDest, nDestWidth, nDestHeight;
   
    const float fImgRatio=(float)mHei/mWid;
    if(fImgRatio > fRatio)
    {
        nDestWidth=(THUMHEIGHT/fImgRatio);
        XDest=(THUMWIDTH-nDestWidth)/2;
        YDest=0;
        nDestHeight=THUMHEIGHT;
    }
    else
    {
        XDest=0;
        nDestWidth=THUMWIDTH;
        nDestHeight=(THUMWIDTH*fImgRatio);
        YDest=(THUMHEIGHT-nDestHeight)/2;
    }
   
    CClientDC cdc(this);
    HDC hDC=::CreateCompatibleDC(cdc.m_hDC);
    HBITMAP bm = CreateCompatibleBitmap(cdc.m_hDC, THUMWIDTH, THUMHEIGHT);
    HBITMAP pOldBitmapImage = (HBITMAP)SelectObject(hDC,bm);
    ::FillRect(hDC, &rcBorder, hBrushBk);
    ::FrameRect(hDC, &rcBorder, hBrushBorder);
    HBITMAP hBmReturn= (HBITMAP)::SelectObject(hDC, pOldBitmapImage);
   
    CDC hdcSrc, hdcDst;
   
    hdcSrc.CreateCompatibleDC(NULL);
    hdcDst.CreateCompatibleDC(NULL);
   
    // Load the bitmaps into memory DC
    CBitmap* hbmSrcT = (CBitmap*) hdcSrc.SelectObject(hPicRet);
    CBitmap* hbmDstT = (CBitmap*) hdcDst.SelectObject(hBmReturn);
   
    hdcDst.BitBlt(XDest,YDest,nDestWidth, nDestHeight, &hdcSrc,0,0,SRCCOPY);   
    pOldBitmapImage = (HBITMAP)SelectObject(hdcDst.m_hDC,bm);
   
    DeleteDC(hDC);
    DeleteObject(hBrushBorder);
    DeleteObject(hBrushBk);
   
    return pOldBitmapImage;
}

CString CAddbmpDlg::GetExeAbsolutePath()
{
    char buffer[ MAX_PATH ];
    CString ExePath = "";
    GetModuleFileName( NULL, buffer, MAX_PATH );
    ExePath=CString( buffer );
    ExePath=ExePath.Left(
                ExePath.ReverseFind( '\\' )>ExePath.ReverseFind( '/' )?
                ExePath.ReverseFind( '\\' )+ 1:
                ExePath.ReverseFind( '/' )+1       
                );
    return ExePath;
}

你可能感兴趣的:(mfc,vc++)