加载图片,更改像素RGB后显示
//Load a bmp file, change its color and display the changed bmp in view
//LoadImage GetDIBits SetDIBits
void CViewBmpView::OnDraw(CDC* pDC)
{//load bmp file:"c:\\temp\\test.bmp"
CViewBmpDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HBITMAP hbmp=(HBITMAP)LoadImage(NULL,"c:\\temp\\test.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);
}
//LoadImage GetDIBits SetDIBits
void CViewBmpView::OnDraw(CDC* pDC)
{//load bmp file:"c:\\temp\\test.bmp"
CViewBmpDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HBITMAP hbmp=(HBITMAP)LoadImage(NULL,"c:\\temp\\test.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);
}