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. How do a capture of a control and save to bitmap file?

How do a capture of a control and save to bitmap file?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformancehelpquestion
11 Posts 2 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.
  • J Offline
    J Offline
    jerome_data
    wrote on last edited by
    #1

    Hello I want to do a capture of one control(custom control) in my project in memory and save directly this capture in a bitmap or jpg file. Help me please. Jdata

    M 1 Reply Last reply
    0
    • J jerome_data

      Hello I want to do a capture of one control(custom control) in my project in memory and save directly this capture in a bitmap or jpg file. Help me please. Jdata

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Using Win32 APIs or MFC? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        Using Win32 APIs or MFC? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        jerome_data
        wrote on last edited by
        #3

        both. i do this CDC dc; CRect rectClient; thecontrol.GetClientRect(&rectClient); HDC hdc = ::GetDC(thecontrol.m_hWnd); dc.Attach(hdc); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap bm; bm.CreateCompatibleBitmap(&dc, rectClient.right, rectClient.bottom); CBitmap * oldbm = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, rectClient.right, rectClient.bottom, &dc, 0, 0, SRCCOPY); here bm as done a screenshot of the control. i want to save this Cbitmap to file .bmp?? If you have other function!! thanks

        M 1 Reply Last reply
        0
        • J jerome_data

          both. i do this CDC dc; CRect rectClient; thecontrol.GetClientRect(&rectClient); HDC hdc = ::GetDC(thecontrol.m_hWnd); dc.Attach(hdc); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap bm; bm.CreateCompatibleBitmap(&dc, rectClient.right, rectClient.bottom); CBitmap * oldbm = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, rectClient.right, rectClient.bottom, &dc, 0, 0, SRCCOPY); here bm as done a screenshot of the control. i want to save this Cbitmap to file .bmp?? If you have other function!! thanks

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Maybe:

          CWindowDC ControlDC(&thecontrol);
          CDC MemoryDC;
          MemoryDC.CreateCompatibleDC(&ControlDC);
          CRect ControlRect;
          thecontrol.GetWindowRect(&ControlRect);
          CImage ControlImage;
          ControlImage.Create(ControlRect.Width(), ControlRect.Height(), 24, 0);
          HGDIOBJ hOldBitmap = ::SelectObject(MemoryDC, (HBITMAP)ControlImage);
          thecontrol.SendMessage(WM_PRINT, (WPARAM)(HDC)MemoryDC, PRF_ERASEBKGND | PRF_CLIENT | PRF_NONCLIENT);
          ::SelectObject(MemoryDC, hOldBitmap);
          ControlImage.Save(_T("c:\\mycontrol.bmp"), ImageFormatBMP);

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          J 1 Reply Last reply
          0
          • M Mark Salsbery

            Maybe:

            CWindowDC ControlDC(&thecontrol);
            CDC MemoryDC;
            MemoryDC.CreateCompatibleDC(&ControlDC);
            CRect ControlRect;
            thecontrol.GetWindowRect(&ControlRect);
            CImage ControlImage;
            ControlImage.Create(ControlRect.Width(), ControlRect.Height(), 24, 0);
            HGDIOBJ hOldBitmap = ::SelectObject(MemoryDC, (HBITMAP)ControlImage);
            thecontrol.SendMessage(WM_PRINT, (WPARAM)(HDC)MemoryDC, PRF_ERASEBKGND | PRF_CLIENT | PRF_NONCLIENT);
            ::SelectObject(MemoryDC, hOldBitmap);
            ControlImage.Save(_T("c:\\mycontrol.bmp"), ImageFormatBMP);

            Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            J Offline
            J Offline
            jerome_data
            wrote on last edited by
            #5

            CImage?? thanks

            M 1 Reply Last reply
            0
            • J jerome_data

              CImage?? thanks

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              CImage is declared in atlimage.h I used that so I wouldn't have to write code to save to a file - let GDI+ do it ;P You could use CBitmap instead (a DIBSection would make it easier) and write your own code to save the bitmap. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              J 2 Replies Last reply
              0
              • M Mark Salsbery

                CImage is declared in atlimage.h I used that so I wouldn't have to write code to save to a file - let GDI+ do it ;P You could use CBitmap instead (a DIBSection would make it easier) and write your own code to save the bitmap. Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                J Offline
                J Offline
                jerome_data
                wrote on last edited by
                #7

                ok i try but i have a error on ImageFormatBMP. undeclared identifier!!! i have done #include to have Cimage class but i have a problem on ImageFormatBMP thanks

                M 1 Reply Last reply
                0
                • M Mark Salsbery

                  CImage is declared in atlimage.h I used that so I wouldn't have to write code to save to a file - let GDI+ do it ;P You could use CBitmap instead (a DIBSection would make it easier) and write your own code to save the bitmap. Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  J Offline
                  J Offline
                  jerome_data
                  wrote on last edited by
                  #8

                  i have a black bmp file (Gdiplus::ImageFormatBMP) ty

                  M 1 Reply Last reply
                  0
                  • J jerome_data

                    ok i try but i have a error on ImageFormatBMP. undeclared identifier!!! i have done #include to have Cimage class but i have a problem on ImageFormatBMP thanks

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    Try Gdiplus::ImageFormatBMP Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • J jerome_data

                      i have a black bmp file (Gdiplus::ImageFormatBMP) ty

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      I didn't know if WM_PRINT would work with your custom control.  I suppose you can scrape it off the screen if it's visible:

                      CWindowDC ScreenDC(0);
                      CDC MemoryDC;
                      MemoryDC.CreateCompatibleDC(&ScreenDC);
                      CRect ControlRect;
                      thecontrol.GetWindowRect(&ControlRect);
                      CImage ControlImage;
                      ControlImage.Create(ControlRect.Width(), ControlRect.Height(), 24, 0);
                      HGDIOBJ hOldBitmap = ::SelectObject(MemoryDC, (HBITMAP)ControlImage);
                      MemoryDC.BitBlt(0, 0, ControlRect.Width(), ControlRect.Height(), &ScreenDC, ControlRect.left, ControlRect.top, SRCCOPY);
                      ::SelectObject(MemoryDC, hOldBitmap);
                      ControlImage.Save(_T("c:\\testmy.bmp"), Gdiplus::ImageFormatBMP);

                      Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      J 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        I didn't know if WM_PRINT would work with your custom control.  I suppose you can scrape it off the screen if it's visible:

                        CWindowDC ScreenDC(0);
                        CDC MemoryDC;
                        MemoryDC.CreateCompatibleDC(&ScreenDC);
                        CRect ControlRect;
                        thecontrol.GetWindowRect(&ControlRect);
                        CImage ControlImage;
                        ControlImage.Create(ControlRect.Width(), ControlRect.Height(), 24, 0);
                        HGDIOBJ hOldBitmap = ::SelectObject(MemoryDC, (HBITMAP)ControlImage);
                        MemoryDC.BitBlt(0, 0, ControlRect.Width(), ControlRect.Height(), &ScreenDC, ControlRect.left, ControlRect.top, SRCCOPY);
                        ::SelectObject(MemoryDC, hOldBitmap);
                        ControlImage.Save(_T("c:\\testmy.bmp"), Gdiplus::ImageFormatBMP);

                        Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        J Offline
                        J Offline
                        jerome_data
                        wrote on last edited by
                        #11

                        It's works Excellent!!! THANKS

                        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