【问题标题】:Bitblt blacknessBitblt 黑度
【发布时间】:2009-10-20 18:11:42
【问题描述】:

我正在运行以下代码,

HDC hdc;
HDC hdcMem;
HBITMAP bitmap;
RECT c;
GetClientRect(viewHandle, &c);
// instead of BeginPaint use GetDC or GetWindowDC
hdc = GetDC(viewHandle); 
hdcMem = CreateCompatibleDC(hdc); 
// always create the bitmap for the memdc from the window dc
bitmap = CreateCompatibleBitmap(hdc,c.right-c.left,200);

SelectObject(hdcMem, bitmap);

// only execute the code up to this point one time
// that is, you only need to create the back buffer once
// you can reuse it over and over again after that

// draw on hdcMem
// for example  ...
Rectangle(hdcMem, 126, 0, 624, 400);

// when finished drawing blit the hdcMem to the hdc
BitBlt(hdc, 0, 0, c.right-c.left,200, hdcMem, 0, 0, SRCCOPY);

// note, height is not spelled i before e

// Clean up - only need to do this one time as well
DeleteDC(hdcMem);
DeleteObject(bitmap);
ReleaseDC(viewHandle, hdc);

代码很好。但我在这个矩形周围看到黑色。这是为什么? Here is an example image.

【问题讨论】:

  • 您应该保存 SelectObject 的结果并在删除之前恢复它,以避免 Windows 问题,您可能会因为 DC 的发布而避免崩溃,但它可能有其他副作用: HBITMAP 保存BM; .. saveBM = SelectObject(hdcMem, bitmap); ... SelectObject(hdcMem, saveBM);删除对象(位图);

标签: winapi bitblt


【解决方案1】:

位图很可能被初始化为全黑。然后,您将在 x 坐标 126 和 624 之间绘制一个白色矩形。因此,x=126 左侧和 x=624 右侧的所有内容都保持黑色。

编辑:documentation for CreateCompatibleBitmap 没有说明如何初始化位图,因此您应该按照 Goz 的建议,使用 FillRect 明确地用特定颜色初始化位图:

RECT rc;

rc.left=0;
rc.top=0;
rc.right=c.right-c.left;
rc.bottom=200;

FillRect(hdcMem, &rc, (HBRUSH)GetStockObject(GRAY_BRUSH));

这个例子用灰色填充位图——如果你需要不同的颜色,你可能需要CreateSolidBrush你自己的画笔。 (完成后别忘了致电DeleteObject。)

作为旁注,我觉得您的位图设置为 200 的恒定高度有点奇怪——正常情况是使位图的高度等于窗口的高度(因为完成宽度)。

【讨论】:

  • 如何初始化指定颜色的位图?
  • 谢谢马丁,它有效。这是正确的方法吗?我想了解我们需要做FillRect的原因。你能在你的回答中解释一下吗?
  • 这是必要的,因为你不知道CreateCompatibleBitmap创建的位图的初始内容是什么;至少 MSDN 文档没有指定位图的初始内容是什么,因此您应该将其初始化为具有定义的行为。
【解决方案2】:

可能是因为您没有将内存位图区域初始化为给定颜色?尝试将背景填充为不同的颜色,然后在其上绘制白色矩形,看看会发生什么。

【讨论】:

    【解决方案3】:

    根据 MSDN http://msdn.microsoft.com/en-us/library/dd162898.aspx:

    矩形使用当前画笔勾勒轮廓并使用当前画笔填充。

    考虑改为致电FillRect,或在致电Rectangle'之前选择合适的笔。

    【讨论】:

    • 即使画一条线也会发生同样的事情。我认为问题出在 BitBlt
    【解决方案4】:

    我用过:

        // Fill the background
        hdcMem->FillSolidRect(c, hdcMem->GetBkColor());
    

    就像一个注释。

    【讨论】:

    • 这是 MFC,不是纯 Win32 API
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 2023-03-09
    • 2014-03-13
    • 2017-09-21
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    相关资源
    最近更新 更多