Image 到 DIB HBITMAP

HBITMAP ImageToBitmap( Gdiplus::Image* image )
{
    HDC MemoryDC = ::CreateCompatibleDC(::GetDC(NULL));

    UINT height = image->GetHeight();
    UINT width = image->GetWidth();
    if (height <= 0 || width <= 0)
    {
        return NULL;
    }

    BITMAPINFO bmpInfo;
    bmpInfo.bmiHeader.biHeight =  -(LONG)height;
    bmpInfo.bmiHeader.biWidth = width;
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 32;
    bmpInfo.bmiHeader.biCompression = BI_RGB;
    bmpInfo.bmiHeader.biSizeImage = 0;
    bmpInfo.bmiHeader.biXPelsPerMeter = 0;
    bmpInfo.bmiHeader.biYPelsPerMeter = 0;
    bmpInfo.bmiHeader.biClrUsed = 0;
    bmpInfo.bmiHeader.biClrImportant = 0;
    BYTE* pbase = NULL;
    HBITMAP memoryBmp = ::CreateDIBSection(MemoryDC, &bmpInfo, DIB_RGB_COLORS, (void**)&pbase, 0, 0);
    if ( !memoryBmp)
    {
        return NULL;
    }

    HBITMAP hOldBmp = (HBITMAP)SelectObject(MemoryDC, memoryBmp);

    Gdiplus::Graphics graphics(MemoryDC);

    Gdiplus::Rect rect(0, 0, width, height);
    graphics.DrawImage(image, rect);

    SelectObject( MemoryDC, hOldBmp);

    return memoryBmp;
}

老忘记, 记录下

原文地址:https://www.cnblogs.com/dazhu/p/2828732.html