cbitmap 获取RGB

CBitMap的用法

 

MFC提供了位图处理的基础类CBitmap,可以完成位图(bmp图像)的创建、图像数据的获取等功能。虽然功能比较少,但是在对位图进行一些简单的处理时,CBitmap类还是可以胜任的。很多人可能会采用一些现成的CDib类进行位图的读取与处理,我觉得必要性不大。因为他们完成的功能差不多。

    以下假设定义了数据成员:

    CBitmap m_bitmap;

(1) 位图的读取

void LoadImage(CString fileName) { HBITMAP m_hBitmap;//位图句柄 BITMAP bm;//存放位图信息的结构

   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关联起来    m_bitmap.GetBitmap(&bm);    height = bm.bmHeight;//图像的宽高    width = bm.bmWidth;

widthBytes=bm.bmWidthBytes;//图像一行数据所占的字节数

}

(2) 位图的显示

在OnDraw()或OnPaint()函数中调用:

//dx0,dy0,dx,dy为显示目标位置的坐标和大小,

//sx0,sy0,sx,sy为图像要显示部分的起始坐标和大小

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);//设置拉伸模式,否则图像会不清晰 dc.StretchBlt(dx0,dy0,dx,dy,&memDC,sx0,sy0,sx,sy,SRCCOPY);//绘制图像 memDC.SelectObject(pbmpOld); memDC.DeleteDC();

}

(3) 位图数据的获取

以灰度转化为例:

int size=height*widthBytes; BYTE *lpBits=new BYTE[size]; m_bitmap.GetBitmapBits(size,lpBits);//得到RGB数据

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;//转化为灰度

   }

...

delete []lpBits;

delete []lpGray;

转载于:https://www.cnblogs.com/chenzuoyou/p/3242444.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个用于C++ MFC开发的Bitmap图片操作类,在文件中叫CBitmapEx,可用于放大,缩小,翻转,过渡和其他有用的功能,有兴趣的朋友可以下载看看。 部分public method: // // void Create(long width, long height); // void Create(CBitmapEx& bitmapEx); // void Load(LPTSTR lpszBitmapFile); // void Save(LPTSTR lpszBitmapFile); // void Scale(long horizontalPercent=100, long verticalPercent=100); // void Rotate(long degrees=0, _PIXEL bgColor=_RGB(0,0,0)); // void FlipHorizontal(); // void FlipVertical(); // void MirrorLeft(); // void MirrorRight(); // void MirrorTop(); // void MirrorBottom(); // void Clear(_PIXEL clearColor=_RGB(0,0,0)); // void Negative(); // void Grayscale(); // void Sepia(long depth=34); // void Emboss(); // void Engrave(); // void Pixelize(long size=4); // void Draw(HDC hDC); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY); // void Draw(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight); // void Draw(long dstX, long dstY, long dstWidth, long dstHeight, CBitmapEx& bitmapEx, // long srcX, long srcY, long srcWidth, long srcHeight, long alpha); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long width, long height, // CBitmapEx& bitmapEx, long srcX, long srcY, long alpha, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // _PIXEL transparentColor=_RGB(0,0,0)); // void DrawTransparent(long dstX, long dstY, long dstWidth, long dstHeight, // CBitmapEx& bitmapEx, long srcX, long srcY, long srcWidth, long srcHeight, // long alpha, _PIXEL transparentColor=_RGB(0,0,0)); // LPBI

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值