MFC. How to display a bitmap or a .png file
-
Good Morning. In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM\_LOENGLISH); pDC->SetWindowExt(800, 800); //pDC->SetViewportOrg(testrec.Width(), testrec.Height()); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { COLORREF color = pBC->getSquare(i, j); CBrush brush(color); int x1 = (j \* 70) + 35; int y1 = (i \* -70) - 35; int x2 = x1 + 70; int y2 = y1 - 70; CRect rect(x1, y1, x2, y2); pDC->FillRect(rect, &brush); } } // Now draw the borders for (int x = 35; x <= 595; x += 70) { pDC->MoveTo(x, -35); pDC->LineTo(x, -595); } for (int y = -35; y >= -595; y -= 70) { pDC->MoveTo(35, y); pDC->LineTo(595, y); }
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CBitmap BmpLady; CDC MemDCLady; // Load the bitmap from the resource BmpLady.LoadBitmap(IDB\_BISHOP\_WHITE); // Create a memory device compatible with the above CPaintDC variable MemDCLady.CreateCompatibleDC(/\*&dc\*/pDC); // Select the new bitmap CBitmap \*BmpPrevious = MemDCLady.SelectObject(&BmpLady); // Copy the bits from the memory DC into the current dc pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY); // Restore the old bitmap pDC->SelectObject(BmpPrevious); // Do not call CView::OnPaint() for painting messages
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display. Any suggestions will be greatly appreciated.
-
Good Morning. In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM\_LOENGLISH); pDC->SetWindowExt(800, 800); //pDC->SetViewportOrg(testrec.Width(), testrec.Height()); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { COLORREF color = pBC->getSquare(i, j); CBrush brush(color); int x1 = (j \* 70) + 35; int y1 = (i \* -70) - 35; int x2 = x1 + 70; int y2 = y1 - 70; CRect rect(x1, y1, x2, y2); pDC->FillRect(rect, &brush); } } // Now draw the borders for (int x = 35; x <= 595; x += 70) { pDC->MoveTo(x, -35); pDC->LineTo(x, -595); } for (int y = -35; y >= -595; y -= 70) { pDC->MoveTo(35, y); pDC->LineTo(595, y); }
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CBitmap BmpLady; CDC MemDCLady; // Load the bitmap from the resource BmpLady.LoadBitmap(IDB\_BISHOP\_WHITE); // Create a memory device compatible with the above CPaintDC variable MemDCLady.CreateCompatibleDC(/\*&dc\*/pDC); // Select the new bitmap CBitmap \*BmpPrevious = MemDCLady.SelectObject(&BmpLady); // Copy the bits from the memory DC into the current dc pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY); // Restore the old bitmap pDC->SelectObject(BmpPrevious); // Do not call CView::OnPaint() for painting messages
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display. Any suggestions will be greatly appreciated.
-
How do you call the
drawImage
method?"In testa che avete, Signor di Ceprano?" -- Rigoletto
// Test to see if I can draw the bitmap on the screen.
// Pass the pDC and a bougus x and y coordinate for testing purposes.
drawImage(pDC, 100, 100); -
// Test to see if I can draw the bitmap on the screen.
// Pass the pDC and a bougus x and y coordinate for testing purposes.
drawImage(pDC, 100, 100);BigSteve-O wrote:
drawImage(pDC, 100, 100);
And where are you calling this method from?
-
Good Morning. In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM\_LOENGLISH); pDC->SetWindowExt(800, 800); //pDC->SetViewportOrg(testrec.Width(), testrec.Height()); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { COLORREF color = pBC->getSquare(i, j); CBrush brush(color); int x1 = (j \* 70) + 35; int y1 = (i \* -70) - 35; int x2 = x1 + 70; int y2 = y1 - 70; CRect rect(x1, y1, x2, y2); pDC->FillRect(rect, &brush); } } // Now draw the borders for (int x = 35; x <= 595; x += 70) { pDC->MoveTo(x, -35); pDC->LineTo(x, -595); } for (int y = -35; y >= -595; y -= 70) { pDC->MoveTo(35, y); pDC->LineTo(595, y); }
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CBitmap BmpLady; CDC MemDCLady; // Load the bitmap from the resource BmpLady.LoadBitmap(IDB\_BISHOP\_WHITE); // Create a memory device compatible with the above CPaintDC variable MemDCLady.CreateCompatibleDC(/\*&dc\*/pDC); // Select the new bitmap CBitmap \*BmpPrevious = MemDCLady.SelectObject(&BmpLady); // Copy the bits from the memory DC into the current dc pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY); // Restore the old bitmap pDC->SelectObject(BmpPrevious); // Do not call CView::OnPaint() for painting messages
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display. Any suggestions will be greatly appreciated.
Restoring the old bitmap should happen on the
MemDCLady
DC where you called the firstSelectObject()
, not the one passed in aspDC
. -
BigSteve-O wrote:
drawImage(pDC, 100, 100);
And where are you calling this method from?
From the onDraw() funtion.
-
Restoring the old bitmap should happen on the
MemDCLady
DC where you called the firstSelectObject()
, not the one passed in aspDC
.I tried the following test, just to see if i can display anything on my board,
CBitmap bitmap; CDC dcMemory; bitmap.LoadBitmap(IDB\_LADYPIC); dcMemory.CreateCompatibleDC(pDC); dcMemory.SelectObject(&bitmap); pDC->BitBlt(100, 200, 54, 96, &dcMemory, 0, 0, SRCCOPY);
The bitmap does not show. I only see my board.
-
Good Morning. In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM\_LOENGLISH); pDC->SetWindowExt(800, 800); //pDC->SetViewportOrg(testrec.Width(), testrec.Height()); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { COLORREF color = pBC->getSquare(i, j); CBrush brush(color); int x1 = (j \* 70) + 35; int y1 = (i \* -70) - 35; int x2 = x1 + 70; int y2 = y1 - 70; CRect rect(x1, y1, x2, y2); pDC->FillRect(rect, &brush); } } // Now draw the borders for (int x = 35; x <= 595; x += 70) { pDC->MoveTo(x, -35); pDC->LineTo(x, -595); } for (int y = -35; y >= -595; y -= 70) { pDC->MoveTo(35, y); pDC->LineTo(595, y); }
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CBitmap BmpLady; CDC MemDCLady; // Load the bitmap from the resource BmpLady.LoadBitmap(IDB\_BISHOP\_WHITE); // Create a memory device compatible with the above CPaintDC variable MemDCLady.CreateCompatibleDC(/\*&dc\*/pDC); // Select the new bitmap CBitmap \*BmpPrevious = MemDCLady.SelectObject(&BmpLady); // Copy the bits from the memory DC into the current dc pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY); // Restore the old bitmap pDC->SelectObject(BmpPrevious); // Do not call CView::OnPaint() for painting messages
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display. Any suggestions will be greatly appreciated.
-
Good Morning. In my view class in the onDraw() function, I am drawing the board and filling in the squares as follows:
pDC->SetMapMode(MM\_LOENGLISH); pDC->SetWindowExt(800, 800); //pDC->SetViewportOrg(testrec.Width(), testrec.Height()); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { COLORREF color = pBC->getSquare(i, j); CBrush brush(color); int x1 = (j \* 70) + 35; int y1 = (i \* -70) - 35; int x2 = x1 + 70; int y2 = y1 - 70; CRect rect(x1, y1, x2, y2); pDC->FillRect(rect, &brush); } } // Now draw the borders for (int x = 35; x <= 595; x += 70) { pDC->MoveTo(x, -35); pDC->LineTo(x, -595); } for (int y = -35; y >= -595; y -= 70) { pDC->MoveTo(35, y); pDC->LineTo(595, y); }
This all works well, and I have my board coming up.
Now I want to draw the chess pieces on the board, and have written some code, just to load 1 bitgmap and display it on the screen. However, nothing gets drawn to the screen. (All I see is just the chessboard)
This is the test code to display my bitmap on the screen:
void CMyChessTestView::drawImage(CDC* pDC, int x, int y)
{CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CBitmap BmpLady; CDC MemDCLady; // Load the bitmap from the resource BmpLady.LoadBitmap(IDB\_BISHOP\_WHITE); // Create a memory device compatible with the above CPaintDC variable MemDCLady.CreateCompatibleDC(/\*&dc\*/pDC); // Select the new bitmap CBitmap \*BmpPrevious = MemDCLady.SelectObject(&BmpLady); // Copy the bits from the memory DC into the current dc pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY); // Restore the old bitmap pDC->SelectObject(BmpPrevious); // Do not call CView::OnPaint() for painting messages
The bitmap is not drwan to the screen. I have tried using both the CPaintDC dc or the pDC* but to no avail. I cannot get the bitmap to display. Any suggestions will be greatly appreciated.
Been a while since I futzed with Windows drawing. There are some tricks to it. Let me go find some old code. Right off the bat, beware working with DCs and bitmaps. MS' implementation can be subtle.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
-
From the onDraw() funtion.
-
Been a while since I futzed with Windows drawing. There are some tricks to it. Let me go find some old code. Right off the bat, beware working with DCs and bitmaps. MS' implementation can be subtle.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
Thank You. Appreciate it.
-
Been a while since I futzed with Windows drawing. There are some tricks to it. Let me go find some old code. Right off the bat, beware working with DCs and bitmaps. MS' implementation can be subtle.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
-
charlieg wrote:
MS' implementation can be subtle.
I have worked with DCs bitmaps, icons etc over many years using MFC, GDI and GDI+, and never had a problem, other than with my own mistakes.
Truth in your statement, but I cut my graphics teeth in X-Windows which seemed to be much more logical than MS' implementation.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
-
A similar code draws the bitmap, on my system. Why didn't you check the return value of the LoadBitmap call?
"In testa che avete, Signor di Ceprano?" -- Rigoletto
It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?
-
It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?
BigSteve-O wrote:
It looks like loadBitmap returns NULL. I have no idea as to why though.
According to Docs in [CBitmap Class | Microsoft Learn](https://learn.microsoft.com/en-us/cpp/mfc/reference/cbitmap-class?view=msvc-170#loadbitmap) :
Quote:
If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns 0.
-
It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?
You can try to use LoadImage API instesd: [LoadImageA function (winuser.h) - Win32 apps | Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadimagea)
-
It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?
-
I used the sample code of this page: CDC Class - CDC::CreateCompatibleDC | Microsoft Learn[^].
"In testa che avete, Signor di Ceprano?" -- Rigoletto
Thank You. I copied the sample code over, and I have noticed the following: 1) If I draw the bitmap first, and then draw the board afterwards, I can see the board and the test bitmap. 2) If I draw the board first and then the test bitmap, I only see the board. The loadBitmap call is successfull)
-
Thank You. Appreciate it.
You still sucking air? I'm coming up on some free time. Turn on your profile to allow direct email.
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.
-
Thank You. I copied the sample code over, and I have noticed the following: 1) If I draw the bitmap first, and then draw the board afterwards, I can see the board and the test bitmap. 2) If I draw the board first and then the test bitmap, I only see the board. The loadBitmap call is successfull)