MFC 中的抗锯齿

Anti aliasing in MFC

我正在尝试在我的 MFC 应用程序中实现抗锯齿功能,我正在使用 this tutorial 中描述的技术。

使用这种方式,在调整大小的位图中绘制它可以工作,但我想创建一个更通用的函数,它接收一个已经绘制好的位图和 return 抗锯齿,我试过这个:

static HBITMAP AntiAliasing(HBITMAP hBitmap)
{
    int escala = 4;

    HBITMAP bmp = __copia(hBitmap); //  Copy the bitmap.

    HDC hMemDC = CreateCompatibleDC(NULL);
    HBITMAP bmpAntigo1 = (HBITMAP)::SelectObject(hMemDC, bmp);

    BITMAP bitmap;
    ::GetObject(hBitmap, sizeof(BITMAP), &bitmap);

    //  Create a bitmap (2x, 4x, 8x) the size of the original bitmap.
    HDC hDCDimensionado = ::CreateCompatibleDC(hMemDC);
    HBITMAP bmpDimensionado = ::CreateCompatibleBitmap(hDCDimensionado, 
                                                        bitmap.bmWidth * escala, 
                                                        bitmap.bmHeight * escala);

    HBITMAP hBmpVelho = (HBITMAP)::SelectObject(hDCDimensionado, bmpDimensionado);

    //  I also tried with {BLACKONWHITE, HALFTONE, WHITEONBLACK}
    int oldStretchBltMode2 = ::SetStretchBltMode(hDCDimensionado, COLORONCOLOR);    

    //  Resize the  bitmap to the new size.
    ::StretchBlt(hDCDimensionado,
        0, 0, bitmap.bmWidth * escala, bitmap.bmHeight * escala, 
        hMemDC, 
        0, 0, bitmap.bmWidth, bitmap.bmHeight, 
        SRCCOPY);

    /*
    *   Here the bitmap has lost his colors and became black and white.
    */

    ::SetStretchBltMode(hDCDimensionado, oldStretchBltMode2);

    //  Set StretchBltMode to halfTone so can mimic the anti aliasing effect.
    int oldStretchBltMode = ::SetStretchBltMode(hMemDC, HALFTONE);

    //  resize to the original size.
    ::StretchBlt(hMemDC, 
        0, 0, bitmap.bmWidth, bitmap.bmHeight, 
        hDCDimensionado, 
        0, 0, escala * bitmap.bmWidth, escala * bitmap.bmHeight, 
        SRCCOPY);

    ::SetStretchBltMode(hMemDC, oldStretchBltMode);
    ::SelectObject(hMemDC, bmpAntigo1);
    ::DeleteDC(hMemDC);
    ::SelectObject(hDCDimensionado, hBmpVelho);
    DeleteDC(hDCDimensionado);

    return bmp;
}

但是这个功能不起作用,结果失去了颜色(所有的图都变成了黑色)并且没有抗锯齿。

任何帮助将不胜感激!

来自 CreateCompatibleBitmap 的文档:

Note: When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected into it. If this memory device context is used in CreateCompatibleBitmap, the bitmap that is created is a monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory device context, as shown in the following code:

更改代码并为桌面提供 hdc,如下所示:

HDC hdc = ::GetDC(0);
HBITMAP bmpDimensionado = ::CreateCompatibleBitmap(hdc, ...)
::ReleaseDC(0, hdc);


这将显示图像,但是这种方法不会产生预期的效果,因为它只是将每个像素放大到更大的尺寸并将其缩小回原始像素。没有与相邻像素混合。

使用其他方法,例如具有高斯模糊效果的 Direct2D,或使用 GDI+ 代替插值模式:

Gdiplus::GdiplusStartup...

void foo(HDC hdc)
{
    Gdiplus::Bitmap bitmap(L"file.bmp");
    if(bitmap.GetLastStatus() != 0)
        return 0;
    auto w = bitmap.GetWidth();
    auto h = bitmap.GetHeight();

    auto maxw = w * 2;
    auto maxh = h * 2;
    Gdiplus::Bitmap membmp(maxw, maxh);
    Gdiplus::Graphics memgr(&membmp);
    memgr.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBilinear);
    memgr.DrawImage(&bitmap, 0, 0, maxw, maxh);

    Gdiplus::Graphics gr(hdc);
    gr.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBilinear);
    gr.DrawImage(&membmp, 0, 0, w, h);
}


如果目标 window 至少是 Vista,请使用具有模糊效果的 GDI+ 1.1 版。另见 How to turn on GDI+ 1.1 in MFC project

#define GDIPVER 0x0110 //add this to precompiled header file

void blur(HDC hdc)
{
    Gdiplus::Graphics graphics(hdc);
    Gdiplus::Bitmap bitmap(L"file.bmp");
    if(bitmap.GetLastStatus() != 0)
        return;

    Gdiplus::Blur blur;
    Gdiplus::BlurParams blur_param;
    blur_param.radius = 3; //change the radius for different result
    blur_param.expandEdge = TRUE;
    blur.SetParameters(&blur_param);
    bitmap.ApplyEffect(&blur, NULL);

    graphics.DrawImage(&bitmap, 0, 0);
}