简体   繁体   中英

Copy HDC content into HBITMAP

can anyone tell me why this code does not copy the HDC into the HBITMAP? I want to have the output of StretchDIBits into a HBITMAP so I can BitBlt the HBITMAP when processing the WM_PAINT message.

HDC myMemHDC = CreateCompatibleDC(NULL);
SetStretchBltMode(myMemHDC, COLORONCOLOR);
StretchDIBits(myMemHDC, 0, 0, global_imagewidth, global_imageheight,
0, 0, FreeImage_GetWidth(global_dib), FreeImage_GetHeight(global_dib),
FreeImage_GetBits(global_dib), FreeImage_GetInfo(global_dib), DIB_RGB_COLORS, SRCCOPY);

HDC myMemHDC2 = CreateCompatibleDC(myMemHDC);
HBITMAP myhbitmap = CreateCompatibleBitmap(myMemHDC2, global_imagewidth, global_imageheight);

HGDIOBJ prevHBITMAP = SelectObject(myMemHDC2, myhbitmap);
BitBlt(myMemHDC2, 0, 0, global_imagewidth, global_imageheight, myMemHDC, 0, 0, SRCCOPY); 
SelectObject(myMemHDC2, prevHBITMAP);

if(global_hddb!=NULL)
{
    DeleteObject(global_hddb);
    global_hddb = myhbitmap;
}
DeleteDC(myMemHDC);
DeleteDC(myMemHDC2);

I think the problem is that the bitmap is selected out of the memory DC. The bitmap is the 'memory' of the memory DC, it has to stay selected.

Define these members in the class or globally:

HDC myMemHDC;
HBITMAP myhbitmap;
HGDIOBJ prevHBITMAP;

Initialize: (App start)

void Init()
{
    myMemHDC = CreateCompatibleDC(NULL);
    myhbitmap = CreateCompatibleBitmap(myMemHDC, global_imagewidth,
        global_imageheight);
    prevHBITMAP = SelectObject(myMemHDC, myhbitmap);

    SetStretchBltMode(myMemHDC, COLORONCOLOR);
    StretchDIBits(myMemHDC, 0, 0, global_imagewidth, global_imageheight,
        0, 0, FreeImage_GetWidth(global_dib), FreeImage_GetHeight(global_dib),
        FreeImage_GetBits(global_dib), FreeImage_GetInfo(global_dib),
        DIB_RGB_COLORS, SRCCOPY);
}

Destroy: (App exit)

void Destroy()
{
    SelectObject(myMemHDC, prevHBITMAP);
    DeleteObject(myhbitmap);
    DeleteDC(myMemHDC);
}

WM_PAINT:

BitBlt(paintDC, 0, 0, global_imagewidth, global_imageheight, myMemHDC, 0, 0, SRCCOPY); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM