替换前:
void CPicColorDlg::OnButton2()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
CDC* pDC = AfxGetApp()->m_pMainWnd->GetDC();
CBitmap bitMap;
HBITMAP hBitMap = (HBITMAP)LoadImage(NULL, "bitmap1.bmp", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
bitMap.Attach(hBitMap);
CDC dcBit;
dcBit.CreateCompatibleDC(pDC);
dcBit.SelectObject(&bitMap);
BITMAP bminfo;
bitMap.GetObject(sizeof(bminfo),&bminfo);
pDC->BitBlt(50,50,bminfo.bmWidth,bminfo.bmHeight,&dcBit,0,0,SRCCOPY);
DeleteObject(bitMap);
}
替换后:
void CPicColorDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CDC* pDC = AfxGetApp()->m_pMainWnd->GetDC();
CBitmap bitMap;
HBITMAP hBitMap = (HBITMAP)LoadImage(NULL, "bitmap1.bmp", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
bitMap.Attach(hBitMap);
CDC dcBit;
dcBit.CreateCompatibleDC(pDC);
dcBit.SelectObject(&bitMap);
BITMAP bminfo;
bitMap.GetObject(sizeof(bminfo),&bminfo);
COLORREF clr,clr2,clr3;
clr = RGB(255,0,0); //被替换的颜色
clr2 = RGB(0,0,255); //替换颜色
for(int i=0; i<bminfo.bmWidth; i++)
{
for(int j=0; j<bminfo.bmHeight; j++)
{
clr3 = dcBit.GetPixel(i,j);
if(clr3==clr)
{
dcBit.SetPixel(i,j,clr2);
}
}
}
pDC->BitBlt(50,50,bminfo.bmWidth,bminfo.bmHeight,&dcBit,0,0,SRCCOPY);
DeleteObject(bitMap);
}