CBitMap的用法 from http://www.cnblogs.com/toconnection/archive/2012/08/04/mfc.html
CBitMap的用法
MFC提供了位圖處理的基礎(chǔ)類(lèi)CBitmap,可以完成位圖(bmp圖像)的創(chuàng)建、圖像數(shù)據(jù)的獲取等功能。雖然功能比較少,但是在對(duì)位圖進(jìn)行一些簡(jiǎn)單的處理時(shí),CBitmap類(lèi)還是可以勝任的。很多人可能會(huì)采用一些現(xiàn)成的CDib類(lèi)進(jìn)行位圖的讀取與處理,我覺(jué)得必要性不大。因?yàn)樗麄兺瓿傻墓δ懿畈欢唷?/p>
??? 以下假設(shè)定義了數(shù)據(jù)成員:
??? CBitmap m_bitmap;
(1) 位圖的讀取
void LoadImage(CString fileName) { HBITMAP m_hBitmap;//位圖句柄 BITMAP bm;//存放位圖信息的結(jié)構(gòu)
?? m_hBitmap = (HBITMAP)::LoadImage(NULL,fileName,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);?? //裝載位圖 ?? if(m_bitmap.m_hObject) ????? m_bitmap.DeleteObject(); ?? m_bitmap.Attach(m_hBitmap);//將句柄與CBitmap關(guān)聯(lián)起來(lái) ?? m_bitmap.GetBitmap(&bm); ?? height = bm.bmHeight;//圖像的寬高 ?? width = bm.bmWidth;
widthBytes=bm.bmWidthBytes;//圖像一行數(shù)據(jù)所占的字節(jié)數(shù)
}
(2) 位圖的顯示
在OnDraw()或OnPaint()函數(shù)中調(diào)用:
//dx0,dy0,dx,dy為顯示目標(biāo)位置的坐標(biāo)和大小,
//sx0,sy0,sx,sy為圖像要顯示部分的起始坐標(biāo)和大小
void DrawImage(int dx0, int dy0, int dx, int dy, int sx0, int sy0, int sx, int sy)
{ if(!m_bitmap.m_hObject) ?? return;
CClientDC dc(this);
CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap* pbmpOld = memDC.SelectObject(&m_bitmap);
dc.SetStretchBltMode(COLORONCOLOR);//設(shè)置拉伸模式,否則圖像會(huì)不清晰 dc.StretchBlt(dx0,dy0,dx,dy,&memDC,sx0,sy0,sx,sy,SRCCOPY);//繪制圖像 memDC.SelectObject(pbmpOld); memDC.DeleteDC();
}
(3) 位圖數(shù)據(jù)的獲取
以灰度轉(zhuǎn)化為例:
int size=height*widthBytes; BYTE *lpBits=new BYTE[size]; m_bitmap.GetBitmapBits(size,lpBits);//得到RGB數(shù)據(jù)
BYTE *lpGray=new BYTE[height*width];
int R, G, B;
for(int i=0;i<height;i++)
?? for(int j=0;j<width;j++)
?? {
????? B=lpBits[i*widthBytes+j*4];
????? G=lpBits[i*widthBytes+j*4+1];
????? R=lpBits[i*widthBytes+j*4+2];
????? lpGray[i*width+j]=(B+G+R)/3;//轉(zhuǎn)化為灰度
?? }
...
delete []lpBits;
delete []lpGray;
轉(zhuǎn)載于:https://www.cnblogs.com/songtzu/archive/2013/01/03/2842580.html
總結(jié)
以上是生活随笔為你收集整理的CBitMap的用法 from http://www.cnblogs.com/toconnection/archive/2012/08/04/mfc.html的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: flash 多个文件上传
- 下一篇: 广发可以全积分兑换吗