将HDC内容复制到HBITMAP

时间:2014-03-21 16:56:55

标签: c++ win32gui

谁能告诉我为什么这段代码不会将HDC复制到HBITMAP中?我希望将StretchDIBits的输出放入HBITMAP中,这样我就可以在处理WM_PAINT消息时对HBITMAP进行BitBlt。

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);

1 个答案:

答案 0 :(得分:0)

我认为问题在于从内存DC中选择了位图。位图是'内存'内存DC,它必须保持选择。

在班级或全球范围内定义这些成员:

HDC myMemHDC;
HBITMAP myhbitmap;
HGDIOBJ prevHBITMAP;

初始化:(应用程序启动)

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);
}

销毁:(应用程序退出)

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

WM_PAINT:

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