Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC. How to display a bitmap or a .png file

MFC. How to display a bitmap or a .png file

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsc++performancetutoriallearning
32 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CPalliniC CPallini

    How do you call the drawImage method?

    "In testa che avete, Signor di Ceprano?" -- Rigoletto

    B Offline
    B Offline
    BigSteve O
    wrote on last edited by
    #3

    // 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);

    V 1 Reply Last reply
    0
    • B BigSteve O

      // 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);

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #4

      BigSteve-O wrote:

      drawImage(pDC, 100, 100);

      And where are you calling this method from?

      B 1 Reply Last reply
      0
      • B BigSteve O

        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.

        G Offline
        G Offline
        Graham Breach
        wrote on last edited by
        #5

        Restoring the old bitmap should happen on the MemDCLady DC where you called the first SelectObject(), not the one passed in as pDC.

        B 1 Reply Last reply
        0
        • V Victor Nijegorodov

          BigSteve-O wrote:

          drawImage(pDC, 100, 100);

          And where are you calling this method from?

          B Offline
          B Offline
          BigSteve O
          wrote on last edited by
          #6

          From the onDraw() funtion.

          CPalliniC 1 Reply Last reply
          0
          • G Graham Breach

            Restoring the old bitmap should happen on the MemDCLady DC where you called the first SelectObject(), not the one passed in as pDC.

            B Offline
            B Offline
            BigSteve O
            wrote on last edited by
            #7

            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.

            1 Reply Last reply
            0
            • B BigSteve O

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              // Restore the old bitmap
              pDC->SelectObject(BmpPrevious);
              // Do not call CView::OnPaint() for painting messages

              I do not think you should be restoring the old bitmap into pDC, since it came out of MemDCLady.

              1 Reply Last reply
              0
              • B BigSteve O

                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.

                C Offline
                C Offline
                charlieg
                wrote on last edited by
                #9

                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.

                B L 2 Replies Last reply
                0
                • B BigSteve O

                  From the onDraw() funtion.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #10

                  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

                  In testa che avete, signor di Ceprano?

                  B 1 Reply Last reply
                  0
                  • C charlieg

                    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.

                    B Offline
                    B Offline
                    BigSteve O
                    wrote on last edited by
                    #11

                    Thank You. Appreciate it.

                    C 3 Replies Last reply
                    0
                    • C charlieg

                      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.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      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.

                      C 1 Reply Last reply
                      0
                      • L Lost User

                        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.

                        C Offline
                        C Offline
                        charlieg
                        wrote on last edited by
                        #13

                        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.

                        1 Reply Last reply
                        0
                        • CPalliniC CPallini

                          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

                          B Offline
                          B Offline
                          BigSteve O
                          wrote on last edited by
                          #14

                          It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?

                          V CPalliniC 3 Replies Last reply
                          0
                          • B BigSteve O

                            It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?

                            V Offline
                            V Offline
                            Victor Nijegorodov
                            wrote on last edited by
                            #15

                            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.

                            1 Reply Last reply
                            0
                            • B BigSteve O

                              It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?

                              V Offline
                              V Offline
                              Victor Nijegorodov
                              wrote on last edited by
                              #16

                              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)

                              1 Reply Last reply
                              0
                              • B BigSteve O

                                It looks like loadBitmap returns NULL. I have no idea as to why though. Do you have a code snippet you can share?

                                CPalliniC Offline
                                CPalliniC Offline
                                CPallini
                                wrote on last edited by
                                #17

                                I used the sample code of this page: CDC Class - CDC::CreateCompatibleDC | Microsoft Learn[^].

                                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                In testa che avete, signor di Ceprano?

                                B 1 Reply Last reply
                                0
                                • CPalliniC CPallini

                                  I used the sample code of this page: CDC Class - CDC::CreateCompatibleDC | Microsoft Learn[^].

                                  "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                  B Offline
                                  B Offline
                                  BigSteve O
                                  wrote on last edited by
                                  #18

                                  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)

                                  CPalliniC 1 Reply Last reply
                                  0
                                  • B BigSteve O

                                    Thank You. Appreciate it.

                                    C Offline
                                    C Offline
                                    charlieg
                                    wrote on last edited by
                                    #19

                                    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.

                                    1 Reply Last reply
                                    0
                                    • B BigSteve O

                                      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)

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #20

                                      That's rather strange. Do you use the same DC for the board and bitmap?

                                      "In testa che avete, Signor di Ceprano?" -- Rigoletto

                                      In testa che avete, signor di Ceprano?

                                      B 1 Reply Last reply
                                      0
                                      • B BigSteve O

                                        Thank You. Appreciate it.

                                        C Offline
                                        C Offline
                                        charlieg
                                        wrote on last edited by
                                        #21

                                        Okay, you have an error in your code: // Copy the bits from the memory DC into the current dc pDC->BitBlt(20, 10, 436, 364, &MemDCLady, 0, 0, SRCCOPY); is the culprit. Recall that you've set your coordinate system to low English which fixes the bassackwardness of drawing. If you trace your board generation, you will see that your Y values go from 0 to negative values. Looking at the above line, you are drawing off the top of your view window. Change the 364 to -364 and you'll see your image. Of course, *I've* never done that :)

                                        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.

                                        1 Reply Last reply
                                        0
                                        • B BigSteve O

                                          Thank You. Appreciate it.

                                          C Offline
                                          C Offline
                                          charlieg
                                          wrote on last edited by
                                          #22

                                          See my other reply.

                                          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.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups