Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Resized bitmap file to an MFC Picture Control

Tags:

c++

mfc

Is there an easier way to do it than this, and if this is the only way, are there any potential memory leaks here?

    CImage img1;
            int dimx = 100, dimy = 100;
    img1.Load(filename);
    //filename = path on local system to the bitmap

    CDC *screenDC = GetDC();
    CDC *pMDC = new CDC;
    pMDC->CreateCompatibleDC(screenDC);

    CBitmap *pb = new CBitmap;
    pb->CreateCompatibleBitmap(screenDC, dimx, dimy);

    CBitmap *pob = pMDC->SelectObject(pb);
    pMDC->SetStretchBltMode(HALFTONE);
    img1.StretchBlt(pMDC->m_hDC,0, 0, dimx, dimy, 0, 0, img1.GetWidth(), img1.GetHeight(), SRCCOPY);
    pMDC->SelectObject(pob);

    CImage new_image;
    new_image.Attach((HBITMAP)(*pb));
    //
    m_pictureCtrl.SetBitmap(new_image.Detach());
    ReleaseDC(screenDC);
like image 698
Vanwaril Avatar asked Feb 26 '10 05:02

Vanwaril


1 Answers

I see no need for the CImage new_image (as SetBitmap takes a HBITMAP which you already have through pb) and pb and pMDC must be deleted (after detaching the HBITMAP), but for the rest it seems correct.

CImage img1;
int dimx = 100, dimy = 100;
img1.Load(filename);
//filename = path on local system to the bitmap

CDC *screenDC = GetDC();
CDC mDC;
mDC.CreateCompatibleDC(screenDC);
CBitmap b;
b.CreateCompatibleBitmap(screenDC, dimx, dimy);

CBitmap *pob = mDC.SelectObject(&b);
mDC.SetStretchBltMode(HALFTONE);
img1.StretchBlt(mDC.m_hDC, 0, 0, dimx, dimy, 0, 0, img1.GetWidth(), img1.GetHeight(), SRCCOPY);
mDC.SelectObject(pob);

m_pictureCtrl.SetBitmap((HBITMAP)b.Detach());
ReleaseDC(screenDC);

Of course I would put the scaling of the CImage/CBitmap into a separate function (make it reusable).

like image 163
Randy Voet Avatar answered Nov 09 '22 23:11

Randy Voet