位比特黑度
我正在运行以下代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
位图很可能被初始化为全黑。然后,您将在 x 坐标 126 和 624 之间绘制一个白色矩形。因此,x=126 左侧和 x=624 右侧的所有内容都保持黑色。
编辑: CreateCompatibleBitmap 的文档 没有说明如何初始化位图,因此您应该按照 Goz 的建议,使用
FillRect
:此示例将位图填充为灰色 - 您可能需要
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, usingFillRect
: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 callDeleteObject
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).
根据 MSDN http://msdn.microsoft.com/en-us/library/ dd162898.aspx:
考虑改为调用
FillRect
,或在调用Rectangle
之前选择合适的笔。Per MSDN http://msdn.microsoft.com/en-us/library/dd162898.aspx:
Consider calling
FillRect
instead, or select an appropriate pen prior to callingRectangle
'.可能是因为您尚未将内存位图区域初始化为给定颜色?尝试将背景填充为不同的颜色,然后在其上绘制白色矩形,看看会发生什么。
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.
我用:
只是作为注释。
I used:
Just as a note.