Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Device Contexts and 24-bit Bitmaps

Status
Not open for further replies.

ASingerMustDie

Programmer
Feb 1, 2001
17
GB
Hi Everyone :),

(Incidentally, I also posted this plea in a Microsoft Newsgroup, I hope that doesn't annoy anyone? :) )

I am having trouble understanding why the following method (invoked when I click the corresponding button in a dialog) is not drawing an image to the screen.

void CMyTestDlg::OnDrawButton()
{
// Array representing a 640 * 480 24-bit bitmap
unsigned char displayImage[640 * 480 * 3];

// Client area of a frame (static) control
CDC *clientDC;
clientDC = GetDlgItem(IDC_DRAW_FRAME)->GetDC();

int pix;
for (int y = 0; y < 480; y++)
{
for (int x = 0; x < 640; x++)
{
// For now I am just setting each RGB (or BGR) component to 50
// until the program works that is
// (Obviously memset would be faster and better otherwise)
pix = (y * 640 * 3) + (x * 3);
displayImage[pix] = 50; // Green
displayImage[pix+1] = 50; // Blue
displayImage[pix+2] = 50; // Red
}
}

BITMAP bm;
bm.bmType = 0;
bm.bmWidth = 640;
bm.bmHeight = 480;
bm.bmWidthBytes = 640 * 3;
bm.bmPlanes = 1;
bm.bmBitsPixel = 24;
bm.bmBits = displayImage;

CBitmap displayBitmap;
displayBitmap.CreateBitmapIndirect(&bm);
// displayBitmap should now be a dark grey 24-bit bitmap

// Create memory based device context and select in the bitmap
CDC MemDC;
MemDC.CreateCompatibleDC(clientDC);
MemDC.SelectObject(&displayBitmap);

// BitBlt the bitmap to the control device context
clientDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &MemDC, 0, 0, SRCCOPY);
}

And what I see is nothing...nothing happens. I have noticed that if I change bm.bmBitsPixel to 16, an image is copied to the control, but it is obviously distorted.

What is the problem?

Thanks much indeed in advance,
Tracy
 
What is Yor Problem really? Do You wish to show (or learn to show) an image or to understand, why Your method not works?
About Your method: it has very many problems, for example:
1. What is IDC_DRAW_FRAME (if Static: on default Static Control hac Icon Type, not Bmp - Type).
2.SelectObject() uses HBITMAB, not BITMAP! To become such handle, You must create a Bitmap - for that You can use Your displayImage[] as a part of DIB.
3. To show a Bitmap in a Window with BitBlt(), You should override OnDraw() Member Function and use BitBlt() with their DC.
4....
Try to look in MSDN (keyword DIBToBitmap)
or use link:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top