gpt4 book ai didi

c++ - 将调整大小的位图文件设置为 MFC 优化校准

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:21 24 4
gpt4 key购买 nike

有没有比这更简单的方法,如果这是唯一的方法,这里是否有任何潜在的内存泄漏?

    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);

最佳答案

我认为不需要 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 的缩放放到一个单独的函数中(使其可重用)。

关于c++ - 将调整大小的位图文件设置为 MFC 优化校准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2339702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com