Setting Resized bitmap file to an MFC Picture Control(将调整大小的位图文件设置为 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);
推荐答案
我认为不需要 CImage new_image(因为 SetBitmap 需要一个您已经通过 pb 获得的 HBITMAP)并且必须删除 pb 和 pMDC(在分离 HBITMAP 之后),但其余的似乎是正确的.
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);
当然,我会将 CImage/CBitmap 的缩放放入一个单独的函数中(使其可重用).
Of course I would put the scaling of the CImage/CBitmap into a separate function (make it reusable).
这篇关于将调整大小的位图文件设置为 MFC 优化校准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将调整大小的位图文件设置为 MFC 优化校准


基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01