谢谢!

解决方案 »

  1.   

    void CChildView::OnToolsMakeBW(void)
    { CWaitCursor wait; if (!imgOriginal.IsIndexed()) { // the image does not use an indexed palette, so we will change each pixel to B&W (slow)
    COLORREF pixel;
    int maxY = imgOriginal.GetHeight(), maxX = imgOriginal.GetWidth();
    byte r,g,b,avg;
    for (int x=0; x<maxX; x++) {
    for (int y=0; y<maxY; y++) {
    pixel = imgOriginal.GetPixel(x,y);
    r = GetRValue(pixel);
    g = GetGValue(pixel);
    b = GetBValue(pixel);
    avg = ((r + g + b)/3);
    imgOriginal.SetPixelRGB(x,y,avg,avg,avg);
    }
    } } else { // the image uses an indexed palette, so we will just change the palette table entries to
    // their B&W equivalents 
    int MaxColors = imgOriginal.GetMaxColorTableEntries();
    RGBQUAD* ColorTable;
    ColorTable = new RGBQUAD[MaxColors]; imgOriginal.GetColorTable(0,MaxColors,ColorTable);
    for (int i=0; i<MaxColors; i++)
    {
    int avg = (ColorTable[i].rgbBlue + ColorTable[i].rgbGreen + ColorTable[i].rgbRed)/3;
    ColorTable[i].rgbBlue = (BYTE)avg;
    ColorTable[i].rgbGreen = (BYTE)avg;
    ColorTable[i].rgbRed = (BYTE)avg;
    }
    imgOriginal.SetColorTable(0,MaxColors,ColorTable);

    delete(ColorTable);
    } Invalidate();
    UpdateWindow();
    }