C++ WIN32:重新缩放 Bitmaps/Giving 位图 HDC

C++ WIN32: Rescaling Bitmaps/Giving Bitmaps HDC's

所以我一直在尝试重新缩放位图而不打印原始图像并重新打印缩放后的图像。我正在尝试使用 StretchBlt(),基于 MSDN Microsoft rescaling images 函数:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162950(v=vs.85).aspx

但这需要与源绑定的辅助 hdc,如果不先打印 HBITMAP 就无法进行拉伸。有没有办法将 HBITMAP 转换为 HDC?我已经能够从 HBITMAP 中获取 HANDLE,这可能提供更直接的路径。我可以做的另一件事是在标准位图中分配的内存(未保存)中创建一个调整大小的位图并打印它。

我打印位图的标准方式是:

HBITMAP hBitmap;
static HANDLE hDIB = NULL;
CHAR szFileName[MAX_PATH] = "fileName.bmp";

hDIB = OpenDIB((LPSTR)szFileName);

hBitmap = BitmapFromDIB(hDIB, NULL);

DrawBitmap(hdc, x, y, hBitmap, SRCCOPY);

我可以尝试的另一个选择是研究显示 bmp 的另一种方法。我是 win32 的新手,所以我不知道完成此任务的任何其他方法。任何关于我如何重新缩放 BITMAP 而无需首先打印它的见解。

您发布的 link (Scaling an Image) already contains code, that renders a bitmap. All you need to do is replace the call to BitBlt with StretchBlt:

BOOL DrawBitmap (HDC hDC, INT x, INT y, INT width, INT height, HBITMAP hBitmap, DWORD dwROP)
{
    HDC       hDCBits;
    BITMAP    Bitmap;
    BOOL      bResult;

    if (!hDC || !hBitmap)
        return FALSE;

    hDCBits = CreateCompatibleDC(hDC);
    GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
    SelectObject(hDCBits, hBitmap);
    // Replace with StretchBlt call
    //bResult = BitBlt(hDC, x, y, Bitmap.bmWidth, Bitmap.bmHeight, hDCBits, 0, 0, dwROP);
    bResult = StretchBlt(hDC, x, y, width, height,
                         hDCBits, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, dwROP);
    DeleteDC(hDCBits);

    return bResult;
}

您可以从 WM_PAINT 消息处理程序中调用它,例如:

case WM_PAINT:
{
    PAINTSTRUCT ps = { 0 };
    HDC hDC = ::BeginPaint( hWnd, &ps );
    RECT rc = { 0 };
    ::GetClientRect( hWnd, &rc );
    DrawBitmap( hDC, 0, 0, rc.right, rc.bottom, hBitmap, SRCCOPY );
    ::EndPaint( hWnd, &ps );
}
break;