void CFigureView::MakeBlackWhiteImage(CImage& pImage, int iType) {//黑白化 CWaitCursor WaitCursor; int Height = pImage.GetHeight();//高度 int Width = pImage.GetWidth();//宽度 if(!pImage.IsIndexed())//Indicates that a bitmap's colors are mapped to an indexed palette {//没有使用调色板 for(int x=0; x<Width; x++) { for(int y=0; y<Height;y++) { COLORREF pixel=pImage.GetPixel(x,y);//Retrieves the color of the pixel at the location specified by x and y. byte r,g,b,Result; r = GetRValue(pixel); g = GetGValue(pixel); b = GetBValue(pixel); switch(iType) { case0: Result = ((r+g+b)/3); break; case1: Result = max(max(r,g),b); break; case2: Result = (2.7*r+0.2*g+0.1*b); break; } pImage.SetPixel(x,y,RGB(Result,Result,Result)); } } } else {//使用调色板 int MaxColors = pImage.GetMaxColorTableEntries();//Retrieves the maximum number of entries in the color table RGBQUAD* ColorTable =new RGBQUAD[MaxColors]; //Retrieves red, green, blue (RGB) color values from a range of entries in the palette of the DIB section pImage.GetColorTable(0,MaxColors,ColorTable); for(int i=0;i<MaxColors;i++) { byte r,g,b,Result; r = ColorTable[i].rgbRed; g = ColorTable[i].rgbGreen; b = ColorTable[i].rgbBlue; switch(iType) { case0: Result = ((r+g+b)/3); break; case1: Result = max(max(r,g),b); break; case2: Result = (2.7*r+0.2*g+0.1*b); break; } ColorTable[i].rgbRed = Result; ColorTable[i].rgbGreen = Result; ColorTable[i].rgbBlue = Result; } pImage.SetColorTable(0,MaxColors,ColorTable); delete ColorTable; } } void CFigureView::OnPsBw() {//图片黑白化 // TODO: 在此添加命令处理程序代码 MakeBlackWhiteImage(m_imageFile,0); CFigureDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDoc->SetModifiedFlag(TRUE);//设置修改标志 Invalidate(); }