16,472
社区成员
发帖
与我相关
我的任务
分享
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);
}