asyncwait
asyncwait

Reputation: 4537

Bitblt blackness

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.

Upvotes: 2

Views: 4288

Answers (4)

Matt Clarkson
Matt Clarkson

Reputation: 14426

I used:

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

Just as a note.

Upvotes: 0

Martin B
Martin B

Reputation: 24140

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

Upvotes: 3

Goz
Goz

Reputation: 62323

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.

Upvotes: 0

Jason Kresowaty
Jason Kresowaty

Reputation: 16500

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'.

Upvotes: 0

Related Questions