获取hbitmap的指定部分。

时间:2014-01-05 10:25:30

标签: winapi visual-c++ bitmap

有一个可以成功显示的HBITMAP。我想获得位图的一部分(由矩形指定),以下代码也是如此,但返回的位图始终是黑色的。代码有问题吗?感谢。

HBITMAP GetSelectedBitmap(HBITMAP p_bitmap, int x, int y, int width, int height){

    HDC l_srcDc = ::CreateCompatibleDC(NULL);
    ::SelectObject(l_srcDc, p_bitmap);

    HDC l_dstDc = ::CreateCompatibleDC(l_srcDc);

    HBITMAP l_newBitmap = CreateCompatibleBitmap(l_dstDc, width, height);
    HBITMAP l_oldBitmap = (HBITMAP)::SelectObject(l_dstDc, l_newBitmap);
    ASSERT(0 != ::BitBlt(l_dstDc, 0, 0, width, height, l_srcDc, x, y, SRCCOPY));
    HBITMAP l_clippedBitmap = (HBITMAP)::SelectObject(l_dstDc, l_oldBitmap);
    ::DeleteDC(l_srcDc);
    ::DeleteDC(l_dstDc);
    return l_clippedBitmap;}

1 个答案:

答案 0 :(得分:0)

我找到了原因,改变了

HBITMAP l_newBitmap = CreateCompatibleBitmap(l_dstDc, width, height);

HBITMAP l_newBitmap = CreateCompatibleBitmap(l_srcDc, width, height);

解决问题。这意味着CreateCompatibleBitmap应该使用源DC。