Keep on going, never give up.

HBITMAP 与 CBitmap的创建与复制

一、HBITMAP复制方法

1、HBITMAP TO CBitmap

CBitmap* pDesBitmap = new CBitmap;
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), "C:/123.BMP", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
pDesBitmap->Attach(hbm);

2、HBITMAP TO HBITMAP

HBITMAP  Cxxx::CopyBitmap(HBITMAP  hSourcehBitmap)
{
CDC sourcedc;
CDC destdc;
// 转载者注:创建空的兼容性dc,其内容是由选入的HBITMAP决定的,
// 但是由GetDC来创建的dc(即null改为GetDC()),其内容则是句柄本所在的窗口内容
sourcedc.CreateCompatibleDC(NULL);
destdc.CreateCompatibleDC(NULL);
//the bitmap information.
BITMAP bm = {0};
//get the bitmap information.
::GetObject(hSourcehBitmap, sizeof(bm), &bm);
// create a bitmap to hold the result
HBITMAP hbmresult = ::CreateCompatibleBitmap(CClientDC(NULL), bm.bmWidth, bm.bmHeight);
HBITMAP hbmoldsource = (HBITMAP)::SelectObject(sourcedc.m_hDC, hSourcehBitmap);
HBITMAP hbmolddest = (HBITMAP)::SelectObject(destdc.m_hDC,  hbmresult);
destdc.BitBlt(0,0,bm.bmWidth, bm.bmHeight, &sourcedc, 0, 0, SRCCOPY);

//   restore   dcs
::SelectObject(sourcedc.m_hDC,  hbmoldsource);
::SelectObject(destdc.m_hDC,  hbmolddest);
::DeleteObject(sourcedc.m_hDC);
::DeleteObject(destdc.m_hDC);

return hbmresult;
}

二、CBitmap复制方法

1、CBitmap to CBitmap

CBitmap* pSrcBitmap = new CBitmap;
CBitmap* pDesBitmap = new CBitmap;
pSrcBitmap->LoadBitmap(IDI_IMG1);//CBitmap不能从文件中读取位图,只能从资源ID读取
HBITMAP hbm = (HBITMAP)pSrcBitmap->m_hObject;//如果不希望清除掉原图资源
//HBITMAP hbm = (HBITMAP)pSrcBitmap->Detach();//如果希望清除掉原图资源
pDesBitmap->Attach(hbm);

2、CBitmap to HBITMAP

 

三、HBITMAP和CBitmap创建

CreateBitmap(8,          //宽8像素
                       3,          //高3像素
                       1,          //单色位图
                       1,          //每个像素的颜色值只占1位
                       bits);     //实际上,是用填充了整个位图。

参考资料(上文部分程序来源地址):

http://blog.csdn.net/fuyanzhi1234/article/details/15501139

http://www.cnblogs.com/ark-zhang/archive/2013/05/10/3071243.html

相关评论(0):  

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

订阅博客

最新文章

本站采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载也必须遵循“署名-非商业用途-保持一致”的创作共用协议. 返回顶部
Copyright@2005-2016 Metsky.com, All rights Reserved.