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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Win32API/MFC to capture desktop as an image

Win32API/MFC to capture desktop as an image

Scheduled Pinned Locked Moved C / C++ / MFC
c++json
14 Posts 4 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.
  • M Madhu_Rani

    I want to capture the desktop as an image just like printscreen function. Any Win32 API or MFC class that perform the task. THANKS.

    A Offline
    A Offline
    Adam Roderick J
    wrote on last edited by
    #2

    i think this link will surely help u :) Various methods for capturing the screen[^]

    Величие не Бога может быть недооценена.

    _ 1 Reply Last reply
    0
    • A Adam Roderick J

      i think this link will surely help u :) Various methods for capturing the screen[^]

      Величие не Бога может быть недооценена.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #3

      I believe there is a better way to do the Capture it the GDI way in the article. Instead of GetDesktopWindow() and GetDC() you can use CreateDC(_T("DISPLAY"), NULL, NULL, NULL);

      «_Superman_» I love work. It gives me something to do between weekends.

      A 1 Reply Last reply
      0
      • _ _Superman_

        I believe there is a better way to do the Capture it the GDI way in the article. Instead of GetDesktopWindow() and GetDC() you can use CreateDC(_T("DISPLAY"), NULL, NULL, NULL);

        «_Superman_» I love work. It gives me something to do between weekends.

        A Offline
        A Offline
        Adam Roderick J
        wrote on last edited by
        #4

        offcourse superman's suggested method is better see the code below for help :) HDC hDesktopDC, hMemDC; int nXPos = 0, nYPos = 0; int nWidth = 0, nHeight = 0; HGDIOBJ hOldBitmap, hBitmap; // check for an empty rectangle if ( IsRectEmpty( lpRect_i )) { return NULL; } // create a DC for the screen and create // a memory DC compatible to screen DC hDesktopDC = CreateDC( _T( "DISPLAY" ), NULL, NULL, NULL ); hMemDC = CreateCompatibleDC( hDesktopDC ); // get points of rectangle to grab nXPos = lpRect_i->left; nYPos = lpRect_i->top; nWidth = lpRect_i->right - lpRect_i->left; nHeight = lpRect_i->bottom - lpRect_i->top ; // create a bitmap compatible with the screen DC hBitmap = CreateCompatibleBitmap( hDesktopDC, nWidth, nHeight ); // select new bitmap into memory DC hOldBitmap = SelectObject ( hMemDC, hBitmap ); // bitblt screen DC to memory DC BitBlt( hMemDC, 0, 0, nWidth, nHeight, hDesktopDC, nXPos, nYPos, SRCCOPY ); // select old bitmap back into memory DC and get handle to // bitmap of the screen hBitmap = SelectObject( hMemDC, hOldBitmap ); // clean up DeleteDC( hDesktopDC ); DeleteDC( hMemDC ); // return handle to the bitmap return ( HBITMAP )hBitmap;

        Величие не Бога может быть недооценена.

        M 1 Reply Last reply
        0
        • A Adam Roderick J

          offcourse superman's suggested method is better see the code below for help :) HDC hDesktopDC, hMemDC; int nXPos = 0, nYPos = 0; int nWidth = 0, nHeight = 0; HGDIOBJ hOldBitmap, hBitmap; // check for an empty rectangle if ( IsRectEmpty( lpRect_i )) { return NULL; } // create a DC for the screen and create // a memory DC compatible to screen DC hDesktopDC = CreateDC( _T( "DISPLAY" ), NULL, NULL, NULL ); hMemDC = CreateCompatibleDC( hDesktopDC ); // get points of rectangle to grab nXPos = lpRect_i->left; nYPos = lpRect_i->top; nWidth = lpRect_i->right - lpRect_i->left; nHeight = lpRect_i->bottom - lpRect_i->top ; // create a bitmap compatible with the screen DC hBitmap = CreateCompatibleBitmap( hDesktopDC, nWidth, nHeight ); // select new bitmap into memory DC hOldBitmap = SelectObject ( hMemDC, hBitmap ); // bitblt screen DC to memory DC BitBlt( hMemDC, 0, 0, nWidth, nHeight, hDesktopDC, nXPos, nYPos, SRCCOPY ); // select old bitmap back into memory DC and get handle to // bitmap of the screen hBitmap = SelectObject( hMemDC, hOldBitmap ); // clean up DeleteDC( hDesktopDC ); DeleteDC( hMemDC ); // return handle to the bitmap return ( HBITMAP )hBitmap;

          Величие не Бога может быть недооценена.

          M Offline
          M Offline
          Madhu_Rani
          wrote on last edited by
          #5

          Thanks all of you for guidance and providing me a couple of alternates to perform the task. I would implement the stuff then I would be back , if i face some problem.....

          M 1 Reply Last reply
          0
          • M Madhu_Rani

            Thanks all of you for guidance and providing me a couple of alternates to perform the task. I would implement the stuff then I would be back , if i face some problem.....

            M Offline
            M Offline
            Madhu_Rani
            wrote on last edited by
            #6

            Sorry, I have to ask very primitive questions but I did some work in VC++ around 9 years back and now forget every thing. Even I have to struggle to play with AfxMessageBox these days:) Anyhow, now I want to display the captured image. The image is captured in HBITMAP hCaptureBitmap, so what class I should use to display the image. Suppose I have SDI based application and in View class what should I do......

            A 1 Reply Last reply
            0
            • M Madhu_Rani

              Sorry, I have to ask very primitive questions but I did some work in VC++ around 9 years back and now forget every thing. Even I have to struggle to play with AfxMessageBox these days:) Anyhow, now I want to display the captured image. The image is captured in HBITMAP hCaptureBitmap, so what class I should use to display the image. Suppose I have SDI based application and in View class what should I do......

              A Offline
              A Offline
              Adam Roderick J
              wrote on last edited by
              #7

              no more direct code Clue :- 1. device context. 2. SelectObject 3. bitBlt Now try yourself :)

              Величие не Бога может быть недооценена.

              M 1 Reply Last reply
              0
              • A Adam Roderick J

                no more direct code Clue :- 1. device context. 2. SelectObject 3. bitBlt Now try yourself :)

                Величие не Бога может быть недооценена.

                M Offline
                M Offline
                Madhu_Rani
                wrote on last edited by
                #8

                You mean , I should get the DC in view class by calling some thing like GetDC , then Select that DC and at the end by using bitBlt should display in the view class.... hmmm. what about if I take an object of Picturebox and then assign the handler of Bitmap to it. but I guess u are right there is no need to take an object of PictureBox, rather view class can be work as a placeholder as well. let me try

                A M 2 Replies Last reply
                0
                • M Madhu_Rani

                  You mean , I should get the DC in view class by calling some thing like GetDC , then Select that DC and at the end by using bitBlt should display in the view class.... hmmm. what about if I take an object of Picturebox and then assign the handler of Bitmap to it. but I guess u are right there is no need to take an object of PictureBox, rather view class can be work as a placeholder as well. let me try

                  A Offline
                  A Offline
                  Adam Roderick J
                  wrote on last edited by
                  #9

                  yes u should try :thumbsup:

                  Величие не Бога может быть недооценена.

                  1 Reply Last reply
                  0
                  • M Madhu_Rani

                    You mean , I should get the DC in view class by calling some thing like GetDC , then Select that DC and at the end by using bitBlt should display in the view class.... hmmm. what about if I take an object of Picturebox and then assign the handler of Bitmap to it. but I guess u are right there is no need to take an object of PictureBox, rather view class can be work as a placeholder as well. let me try

                    M Offline
                    M Offline
                    Madhu_Rani
                    wrote on last edited by
                    #10

                    void CSDITESTView::OnFileTest() { int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); HWND hDesktopWnd = GetDesktopWindow()->m_hWnd; HDC hDesktopDC = ::GetDC(hDesktopWnd); HDC hCaptureDC = CreateCompatibleDC(hDesktopDC); HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight); SelectObject(hCaptureDC,hCaptureBitmap); BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); ::ReleaseDC(hDesktopWnd,hDesktopDC); DeleteDC(hCaptureDC); } I guess this code should work as it is doing the all stuff already, but it not displaying anything ? why

                    A 1 Reply Last reply
                    0
                    • M Madhu_Rani

                      void CSDITESTView::OnFileTest() { int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); HWND hDesktopWnd = GetDesktopWindow()->m_hWnd; HDC hDesktopDC = ::GetDC(hDesktopWnd); HDC hCaptureDC = CreateCompatibleDC(hDesktopDC); HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight); SelectObject(hCaptureDC,hCaptureBitmap); BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); ::ReleaseDC(hDesktopWnd,hDesktopDC); DeleteDC(hCaptureDC); } I guess this code should work as it is doing the all stuff already, but it not displaying anything ? why

                      A Offline
                      A Offline
                      Adam Roderick J
                      wrote on last edited by
                      #11

                      Who will call the paint? clue 1. Some one should call painting, isnt :) Just simple, usually it is not done to give the whole code, well check the below code. HBITMAP CopyScreen( LPRECT lpRect_i ) { HDC hDesktopDC, hMemDC; int nXPos = 0, nYPos = 0; int nWidth = 0, nHeight = 0; HGDIOBJ hOldBitmap, hBitmap; // check for an empty rectangle if ( IsRectEmpty( lpRect_i )) { return NULL; } // create a DC for the screen and create // a memory DC compatible to screen DC hDesktopDC = CreateDC( _T( "DISPLAY" ), NULL, NULL, NULL ); hMemDC = CreateCompatibleDC( hDesktopDC ); // get points of rectangle to grab nXPos = lpRect_i->left; nYPos = lpRect_i->top; nWidth = lpRect_i->right - lpRect_i->left; nHeight = lpRect_i->bottom - lpRect_i->top ; // create a bitmap compatible with the screen DC hBitmap = CreateCompatibleBitmap( hDesktopDC, nWidth, nHeight ); // select new bitmap into memory DC hOldBitmap = SelectObject ( hMemDC, hBitmap ); // bitblt screen DC to memory DC BitBlt( hMemDC, 0, 0, nWidth, nHeight, hDesktopDC, nXPos, nYPos, SRCCOPY ); // select old bitmap back into memory DC and get handle to // bitmap of the screen hBitmap = SelectObject( hMemDC, hOldBitmap ); // clean up DeleteDC( hDesktopDC ); DeleteDC( hMemDC ); // return handle to the bitmap return ( HBITMAP )hBitmap; } void CSlideshawDlg::OnFirst() { RECT rect; rect.left = 150; rect.bottom = 500; rect.top = 150; rect.right = 500; HBITMAP hBit = CopyScreen( &rect ); CDC memdc1; CClientDC dc(this); memdc1.CreateCompatibleDC(&dc); memdc1.SelectObject( hBit ); dc.BitBlt(150,150,500,500,&memdc1,0,0,SRCCOPY); }

                      Величие не Бога может быть недооценена.

                      modified on Thursday, July 30, 2009 7:01 AM

                      M 1 Reply Last reply
                      0
                      • A Adam Roderick J

                        Who will call the paint? clue 1. Some one should call painting, isnt :) Just simple, usually it is not done to give the whole code, well check the below code. HBITMAP CopyScreen( LPRECT lpRect_i ) { HDC hDesktopDC, hMemDC; int nXPos = 0, nYPos = 0; int nWidth = 0, nHeight = 0; HGDIOBJ hOldBitmap, hBitmap; // check for an empty rectangle if ( IsRectEmpty( lpRect_i )) { return NULL; } // create a DC for the screen and create // a memory DC compatible to screen DC hDesktopDC = CreateDC( _T( "DISPLAY" ), NULL, NULL, NULL ); hMemDC = CreateCompatibleDC( hDesktopDC ); // get points of rectangle to grab nXPos = lpRect_i->left; nYPos = lpRect_i->top; nWidth = lpRect_i->right - lpRect_i->left; nHeight = lpRect_i->bottom - lpRect_i->top ; // create a bitmap compatible with the screen DC hBitmap = CreateCompatibleBitmap( hDesktopDC, nWidth, nHeight ); // select new bitmap into memory DC hOldBitmap = SelectObject ( hMemDC, hBitmap ); // bitblt screen DC to memory DC BitBlt( hMemDC, 0, 0, nWidth, nHeight, hDesktopDC, nXPos, nYPos, SRCCOPY ); // select old bitmap back into memory DC and get handle to // bitmap of the screen hBitmap = SelectObject( hMemDC, hOldBitmap ); // clean up DeleteDC( hDesktopDC ); DeleteDC( hMemDC ); // return handle to the bitmap return ( HBITMAP )hBitmap; } void CSlideshawDlg::OnFirst() { RECT rect; rect.left = 150; rect.bottom = 500; rect.top = 150; rect.right = 500; HBITMAP hBit = CopyScreen( &rect ); CDC memdc1; CClientDC dc(this); memdc1.CreateCompatibleDC(&dc); memdc1.SelectObject( hBit ); dc.BitBlt(150,150,500,500,&memdc1,0,0,SRCCOPY); }

                        Величие не Бога может быть недооценена.

                        modified on Thursday, July 30, 2009 7:01 AM

                        M Offline
                        M Offline
                        Madhu_Rani
                        wrote on last edited by
                        #12

                        This is what I cooked, but it is not working... I know there is Invalidate() but it call onpaint again, so why we call it here. void CSDITESTView::OnFileTest() { //m_pDC store the CDC pointer in OnDraw() function. CDeskTop desktop; CBitmap bitmap; HWND hWnd=NULL; bitmap.Attach(desktop.GetDeskTopImage()); BITMAP bm; bitmap.GetBitmap(&bm ); CDC memDC; CClientDC dc(this); memDC.CreateCompatibleDC(&dc); memDC.SelectObject(&bitmap ); m_pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0,SRCCOPY); } HBITMAP CDeskTop:: GetDeskTopImage() { int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); HWND hDesktopWnd = GetDesktopWindow(); HDC hDesktopDC = GetDC(hDesktopWnd); HDC hCaptureDC = CreateCompatibleDC(hDesktopDC); HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight); SelectObject(hCaptureDC,hCaptureBitmap); BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); ReleaseDC(hDesktopWnd,hDesktopDC); DeleteDC(hCaptureDC); return hCaptureBitmap; }

                        M 1 Reply Last reply
                        0
                        • M Madhu_Rani

                          This is what I cooked, but it is not working... I know there is Invalidate() but it call onpaint again, so why we call it here. void CSDITESTView::OnFileTest() { //m_pDC store the CDC pointer in OnDraw() function. CDeskTop desktop; CBitmap bitmap; HWND hWnd=NULL; bitmap.Attach(desktop.GetDeskTopImage()); BITMAP bm; bitmap.GetBitmap(&bm ); CDC memDC; CClientDC dc(this); memDC.CreateCompatibleDC(&dc); memDC.SelectObject(&bitmap ); m_pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0,SRCCOPY); } HBITMAP CDeskTop:: GetDeskTopImage() { int nScreenWidth = GetSystemMetrics(SM_CXSCREEN); int nScreenHeight = GetSystemMetrics(SM_CYSCREEN); HWND hDesktopWnd = GetDesktopWindow(); HDC hDesktopDC = GetDC(hDesktopWnd); HDC hCaptureDC = CreateCompatibleDC(hDesktopDC); HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,nScreenWidth, nScreenHeight); SelectObject(hCaptureDC,hCaptureBitmap); BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight, hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); ReleaseDC(hDesktopWnd,hDesktopDC); DeleteDC(hCaptureDC); return hCaptureBitmap; }

                          M Offline
                          M Offline
                          Madhu_Rani
                          wrote on last edited by
                          #13

                          :) Now I got the solution. m_pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0,SRCCOPY); dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0,SRCCOPY); I replaced m_pDC with dc. and now it works. THANKS A LOT INDEED FOR YOUR CONTINOUS HELP

                          A 1 Reply Last reply
                          0
                          • M Madhu_Rani

                            :) Now I got the solution. m_pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0,SRCCOPY); dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0,SRCCOPY); I replaced m_pDC with dc. and now it works. THANKS A LOT INDEED FOR YOUR CONTINOUS HELP

                            A Offline
                            A Offline
                            Asharudeed
                            wrote on last edited by
                            #14

                            Hi.. Basically I am working with CPP console application. Could any one update me, to capture screenshot of Desktop (by using the code mentioned in this thread).. whether I need to use MFC project or ATL based Project. Any guidelines will help me a lot. Thanks in advance.

                            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