Thread: Taking a screenshot

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    8

    Taking a screenshot

    I've been looking for awhile, and came up with this code.
    Code:
    void CaptureScreen()
    {
        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); 
         //Here I need some code to save the image.
        ReleaseDC(hDesktopWnd,hDesktopDC);
        DeleteDC(hCaptureDC);
        DeleteObject(hCaptureBitmap);
    }
    Can someone please fill in the missing part for me?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can get an imaging API, or you could do it yourself by getting the file format at http://www.wotsit.org.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    8
    What's an imaging api?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Application Programming Interface. It'll just be a set of functions you can download in the form of a library, so that you can easily add functionality into your programming. For example, instead of getting into a lot of REALLY difficult assembly and hard work, there is an intuitive (or reasonably so) API for Windows Programming. It has functions for all the tasks related to windowing, etc... An imaging API would allow you to write to pictures a lot easier than if you had to write directly to the file in JPEG format.

    If you can not find an API or would just like a bit of a challenge, check out wotsit.org and see how JPEG files work, and then you can figure out how to write it yourself. Easier than it sounds. Maybe.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    8
    I'm in over my head. Can you please help me out with finishing that code?

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Here.

    In future, please direct your windows specific questions to the windows board.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    8
    That code won't compile in dev-c++.

    Edit: I get these errors
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x21):Untitled1.cpp: undefined reference to `CreateCompatibleDC@4'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x5c):Untitled1.cpp: undefined reference to `CreateCompatibleBitmap@12'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x71):Untitled1.cpp: undefined reference to `SaveDC@4'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x8f):Untitled1.cpp: undefined reference to `SelectObject@8'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0xf8):Untitled1.cpp: undefined reference to `BitBlt@36'

    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x1d1):Untitled1.cpp: undefined reference to `BitBlt@36'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x201):Untitled1.cpp: undefined reference to `RestoreDC@8'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x211):Untitled1.cpp: undefined reference to `DeleteDC@4'
    C:\DOCUME~1\JAREDJ~1\LOCALS~1\Temp/ccKibaaa.o(.text+0x221):Untitled1.cpp: undefined reference to `DeleteObject@4'

    Execution terminated
    Last edited by Jareds411; 09-15-2004 at 06:55 PM.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by Jareds411
    I'm in over my head.
    Looks like you need to take a step back and read some tutorials/books - search the windows board as there have been many recommendations made there in the past regarding such resources.
    Quote Originally Posted by Jared411
    That code won't compile in dev-c++.
    It most certainly will, provided you build it as a windows application and not as a console; those 'undefined references' are linker errors regarding windows specific libraries.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Taking a screenshot of an application.
    By Cogman in forum Linux Programming
    Replies: 7
    Last Post: 12-11-2008, 12:40 PM
  2. taking screenshot
    By qwertylol2 in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2007, 12:26 AM
  3. Taking Screenshot - Full Screen Dos
    By loko in forum C Programming
    Replies: 12
    Last Post: 07-16-2005, 01:23 AM
  4. Taking a screenshot in DirectDraw
    By Stan100 in forum Game Programming
    Replies: 3
    Last Post: 11-13-2003, 11:26 PM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM