有没有比这更简单的方法,如果这是唯一的方法,这里是否有潜在的内存泄漏?
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);
答案 0 :(得分:8)
我认为没有必要使用CImage new_image(因为SetBitmap通过pb获取了已经拥有的HBITMAP),并且必须删除pb和pMDC(在分离HBITMAP之后),但其余的看起来是正确的。
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);
当然,我会将CImage / CBitmap的缩放放到一个单独的函数中(使其可重用)。