位比特黑度

发布于 2024-08-05 15:09:54 字数 1036 浏览 8 评论 0原文

我正在运行以下代码,

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

代码很好。但我在这个矩形周围看到黑色。这是为什么? 这是一个示例图片。

I am running this following code,

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

The code is just fine. But I am seeing black color around this rectangle. Why is that? Here is an example image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

偏爱你一生 2024-08-12 15:09:54

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

编辑: 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 的恒定高度有点奇怪 - 正常的事情是使位图的高度等于窗口的高度(与宽度相同)。

The bitmap is most likely initialized to be all black. You are then drawing a white rectangle that between x-coordinates 126 and 624. Hence, everything to the left of x=126 and to the right of x=624 stays black.

Edit: The documentation for CreateCompatibleBitmap doesn't state how the bitmap will be initialized, so you should explicitly initialize the bitmap with a specific colour, as Goz suggests, using 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));

This example fills the bitmap in gray -- you may need to CreateSolidBrush your own brush if you need a different colour. (Don't forget to call DeleteObject when you're done.)

As a side note, I find it a bit strange that your bitmap is being set to a constant height of 200 -- the normal thing would be to make the height of the bitmap equal to the height of the window (as is done for the width).

何止钟意 2024-08-12 15:09:54

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

使用当前笔绘制矩形轮廓并使用当前画笔填充矩形。

考虑改为调用 FillRect,或在调用 Rectangle 之前选择合适的笔。

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

The rectangle is outlined by using the current pen and filled by using the current brush.

Consider calling FillRect instead, or select an appropriate pen prior to calling Rectangle'.

对风讲故事 2024-08-12 15:09:54

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

Might it be because you haven't initialised the memory bitmap area to a given colour? Try FillRect'ing the background to a different colour then draw your white rectangle over it and see what happens.

岁月无声 2024-08-12 15:09:54

我用:

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

只是作为注释。

I used:

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

Just as a note.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文