Home > database >  how to mirror a HBITMAP
how to mirror a HBITMAP

Time:04-01

how to flip a HBITMAP horizontally?As an option, I thought to get an array of colors from BITMAP and write them to another BITMAP, but somehow it's too busy. Are there built-in functions or other options to do this?

CodePudding user response:

You can use StretchBlt with a negative dimension as below:

HBITMAP FlipBitmapHorizontally(HBITMAP hbm) {
    BITMAP bm;
    GetObject(hbm, sizeof(BITMAP), &bm);
    int wd = bm.bmWidth;
    int hgt = bm.bmHeight;

    HDC hdcScr = GetDC(NULL);
    HDC hdcFlipped = CreateCompatibleDC(hdcScr);
    HBITMAP hbmFlipped = CreateCompatibleBitmap(hdcScr, wd, hgt);
    auto oldFlipped = SelectObject(hdcFlipped, hbmFlipped);
    HDC hdcSrc = CreateCompatibleDC(hdcScr);
    auto oldSrc = SelectObject(hdcSrc, hbm);

    StretchBlt(hdcFlipped, wd, 0, -wd, hgt, hdcSrc, 0, 0, wd, hgt, SRCCOPY);

    SelectObject(hdcSrc, oldSrc);
    DeleteDC(hdcSrc);
    SelectObject(hdcFlipped, oldFlipped);
    DeleteDC(hdcFlipped);
    ReleaseDC(NULL, hdcScr);

    return hbmFlipped;
} 
  • Related