怎么将指定位置的bmp图片像素值保存到一个二维数组中
如何将指定位置的bmp图片像素值保存到一个二维数组中?
如题,想要设计一个按钮,按下以后,就可以读取硬盘上指定位置的bmp图片像素值保存到一个二维数组(图宽*图高)中,要怎样实现呢?
我参考了论坛里别人的帖子http://topic.****.net/t/20020309/20/565606.html,可是它提供的程序汇报错。
附上我的代码:
请高手指教~~~
------解决方案--------------------
jf~~^_^
如题,想要设计一个按钮,按下以后,就可以读取硬盘上指定位置的bmp图片像素值保存到一个二维数组(图宽*图高)中,要怎样实现呢?
我参考了论坛里别人的帖子http://topic.****.net/t/20020309/20/565606.html,可是它提供的程序汇报错。
附上我的代码:
- C/C++ code
void CImageBoardView::OnTest() { //load bmp file:"d:\\lego1.bmp" CDC* pDC; pDC=GetDC( ); CImageBoardDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here HBITMAP hbmp=(HBITMAP)LoadImage(NULL,"d:\\lego1.bmp",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE); CBitmap cbmp; cbmp.Attach(hbmp); BITMAP bmp; cbmp.GetBitmap(&bmp); cbmp.Detach(); UINT * pData = new UINT[bmp.bmWidth * bmp.bmHeight]; BITMAPINFO bmpInfo; bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biWidth = bmp.bmWidth; bmpInfo.bmiHeader.biHeight = -bmp.bmHeight; bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biCompression = BI_RGB; bmpInfo.bmiHeader.biBitCount = 32; GetDIBits(pDC->m_hDC,hbmp,0,bmp.bmHeight,pData,&bmpInfo,DIB_RGB_COLORS); UINT color, r, g, b; for(int i = 0; i < bmp.bmWidth * bmp.bmHeight; i ++) { color = pData[i]; b = color << 8 >> 24; g = color << 16 >> 24; r = color << 24 >> 24; //note that infact, the r is Blue, b = Red, r = 0;//mask the blue bits pData[i] = RGB(r, g, b); } SetDIBits(pDC->m_hDC, hbmp,0, bmp.bmHeight, pData,&bmpInfo, DIB_RGB_COLORS); CDC dcmem; dcmem.CreateCompatibleDC(pDC); HGDIOBJ hold=::SelectObject(dcmem.m_hDC,hbmp); pDC->BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&dcmem,0,0,SRCCOPY); ::SelectObject(dcmem.m_hDC,hold); delete pData; DeleteObject(hbmp); CString sos; //测试输出像素数组之一 sos.Format("%d",pData[1]); AfxMessageBox(sos); }
请高手指教~~~
------解决方案--------------------
jf~~^_^