拷贝一个位图的任意矩形区域生成新的位图,实际调试发现什么也没拷贝出来,请大家帮忙.
程序如下:HBITMAP ImageControl::CopyBitmap(HBITMAP hbmSrc, int x, int y, int width, int height)
{ HBITMAP hbmDst;
HDC hdcOrg;
HDC hdcSrcMem, hdcDstMem;
RECT rt, rt1; hdcOrg = GetDC(hWnd); hdcSrcMem = CreateCompatibleDC(hdcOrg);
SelectObject(hdcSrcMem, hbmSrc); hdcDstMem = CreateCompatibleDC(hdcOrg);
hbmDst = CreateCompatibleBitmap(hdcDstMem, width, height);
SelectObject(hdcDstMem, hbmDst); ReleaseDC(hWnd, hdcOrg); BitBlt(hdcDstMem, 0, 0, width, height, hdcSrcMem, x, y, SRCCOPY);
GetClientRect(hWnd, &rt);/*
rt1.left = rt.left + 10;
rt1.top = rt.top + 5;
rt1.right = rt.right - 10;
rt1.bottom = rt.bottom - 5; FrameRect(hdcDstMem, &rt1, (HBRUSH)GetStockObject(WHITE_BRUSH));
FrameRect(hdcDstMem, &rt, (HBRUSH)GetStockObject(WHITE_BRUSH));
*/
DeleteDC(hdcSrcMem);
DeleteDC(hdcDstMem);

return hbmDst;
}

解决方案 »

  1.   

    把这一句  hbmDst = CreateCompatibleBitmap(hdcDstMem, width, height);
    改成
              hbmDst = CreateCompatibleBitmap(hdcOrg, width, height);
    试试
      

  2.   

    试过了,不过什么都没有了,连下边的测试矩形框都画不出来了。 rt1.left = rt.left + 10;
    rt1.top = rt.top + 5;
    rt1.right = rt.right - 10;
    rt1.bottom = rt.bottom - 5; FrameRect(hdcDstMem, &rt1, (HBRUSH)GetStockObject(WHITE_BRUSH));
    FrameRect(hdcDstMem, &rt, (HBRUSH)GetStockObject(WHITE_BRUSH));
      

  3.   

    知道了,是我弄错了。我调整了顺序,把ReleaseDC(hWnd, hdcOrg)放到了
    hbmDst = CreateCompatibleBitmap(hdcDstMem, width, height)前面。现在把hdcDstMem换成hdcDlg然后再把ReleaseDC(hWnd, hdcOrg)放在挪回来就可以了,多谢yaozijian110!