Paul Varghese
Paul Varghese

Reputation: 1655

Displaying multiple bitmaps in MFC

I'm trying to display two bitmaps of the same image in the view at different location as shown below, but it's showing only the first one. If I comment out the first one then the other one is displayed.

void CChildView::OnPaint() 
{
  // Load the bitmap
  CBitmap BmpLady;
  // Load the bitmap from the resource
  BmpLady.LoadBitmap(IDB_MB);

  CPaintDC dc(this); // device context for painting

  // Create a memory device compatible with the above CPaintDC variable
  CDC MemDCLady;
  MemDCLady.CreateCompatibleDC(&dc);

  // Select the new bitmap
  CBitmap *BmpPrevious = MemDCLady.SelectObject(&BmpLady);
  // Copy the bits from the memory DC into the current dc
  dc.BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY);
  // Restore the old bitmap
  dc.SelectObject(BmpPrevious);

  // Draw another bitmap for same image.
  CPaintDC dc1(this);

  CDC MemDCLady1;
  MemDCLady1.CreateCompatibleDC(&dc1);
  CBitmap *BmpPrevious1 = MemDCLady1.SelectObject(&BmpLady);
  dc1.BitBlt(200, 100, 436, 364, &MemDCLady1, 0, 0, SRCCOPY);
  dc1.SelectObject(BmpPrevious1);
}

How to display both the images simultaneously? Please help. Thanks in advance.

P.S: I'm fairly new to MFC.

Upvotes: 1

Views: 1957

Answers (1)

Paul Varghese
Paul Varghese

Reputation: 1655

There is no need to CreateCompatibleDC again for the second bitmap. With the following changes I'm able to display both the bitmaps simultaneously

void CChildView::OnPaint() 
{
  CBitmap BmpLady;
  // Load the bitmap from the resource
  BmpLady.LoadBitmap(IDB_MB);

  CPaintDC dc(this);
  CDC MemDCLady;

  // Create a memory device compatible with the above CPaintDC variable
  MemDCLady.CreateCompatibleDC(&dc);
  // Select the new bitmap
  //CBitmap *BmpPrevious = MemDCLady.SelectObject(&BmpLady);
   MemDCLady.SelectObject(&BmpLady);
  // Copy the bits from the memory DC into the current dc
  dc.BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY);

  // MemDCLady.SelectObject(&BmpLady);
  // Copy the bits from the memory DC into the current dc
  dc.BitBlt(200, 100, 436, 364, &MemDCLady, 0, 0, SRCCOPY);
}

Upvotes: 3

Related Questions