Thread: Capturing screen image ?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    14

    Capturing screen image ?

    someone could help me with a tutorial, or a help where to start ?

    thanks in advance

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    What do you plan on doing with the image? You want it in a buffer to play with? Save to a file? Want it in a BITMAP? DirectX, WinAPIs?
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Everything is in MSDN
    0.05 seconds to find code with Google

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    At the begining i need it in a buffer as pixels, and go with a for cycle, and look for how many colors is being used...

    but Brad0407 what about that WinAPI's... ??? which API's could i use ???

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Here's a hint: You can treat the screen as a whole like you would any other window.
    Code:
    HDC screenDC = GetDC(0);
    There are so many different ways to use the APIs that you should probably decide which ones to use for yourself. I assume you at least know how to draw a bitmap cause that will be the easiest thing you'll do.
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    I found this cod as a example:

    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|CAPTUREBLT); 
        SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code
                                    //here to save the captured image to disk
        ReleaseDC(hDesktopWnd,hDesktopDC);
        DeleteDC(hCaptureDC);
        DeleteObject(hCaptureBitmap);
    }
    is this HDC hDesktopDC = GetDC(hDesktopWnd); where will hold all the pixels, and if i wan to do a cycle for and check all the pixels how would i do it ?

    for (i = 0; i <= hDesktopDC; i++ ) something like this ?

    then what i need is to check each pixel color... how could I do this ??

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    That depends. Is speed important to you? GetPixel() is very slow but it's really easy. You can use GetBitmapBits or something like that if speed is important but I always have trouble getting it to work. If speed is of the utmost importance, use DirectX.
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Yes speed is very important to me,

    I need to use something like GetBitmapBits(hDesktopDC) to get the pixels in a array ???

    so when you say use directX i should change all the code or i just use directX to grap info from hDesktopDC and then use it with directX functions to retrieve information about the pixels, i'm a bit confuse ???

  9. #9

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    well, i will try to look for something about dib section, but if you could leave more information about it, would be great

  11. #11
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    If you use a back buffer, why not just set the bitmap file header data then write the contents of the back buffer after this?

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    didnt't understand why you saying about back buffer, i want to say im a novice at C++, i know the realy basic, and i found the code above from a a tutorial.

    everything someone says here i try to find more information about it, on google, but i appreciate if someone could leave examples when talking about the stuff i could do thanks

  13. #13
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Hmm. Do you have a back bufffer? A back buffer is a secondary place used to compile a scene then when the scene is done, it is drawn on the screen. Although not important for all programs, a back buffer is useful for games and some other types of programs. Without it, you may be seeing odd flickering effects from two images being drawn to the screen at different times and the monitor's refresh getting to the area between the drawings. If you use a back buffer, taking a screenshot is rather easy.

    I'm also fairly new to C, but not entirely new to programming. I'd suggest looking up BITMAPFILEHEADER and BITMAPINFOHEADER to get the header details, and using fwrite (and the related) to write the output. The file header comes first, then the info header, then the image data itself.

    My methods may not be anything standard, I mainly just use whatever works and meets my needs.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Did you click the link? What additional information were you after?

  15. #15
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I have added some comments to this code.

    Quote Originally Posted by caricas View Post
    I found this cod as a example:

    Code:
    Void CaptureScreen()
    {
        int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
        int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
        HWND hDesktopWnd = GetDesktopWindow();    Not required
        HDC hDesktopDC = GetDC(hDesktopWnd);   Use NULL to get the desktop HDC 
        HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
        HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC, 
                                nScreenWidth, nScreenHeight);
        SelectObject(hCaptureDC,hCaptureBitmap);  the current GDI object should be saved as per below
        HBITMAP hOriginalBMP=SelectObject(hCaptureDC,hCaptureBitmap); 
        BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
               hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); 
        SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code
                                    //here to save the captured image to disk
        ReleaseDC(hDesktopWnd,hDesktopDC); 
        DeleteDC(hCaptureDC);   the original 1x1 BMP should be SelectObject() back before trying to delete 
        DeleteObject(hCaptureBitmap);  you should not delete a GDI object while it is selected into a HDC 
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Render text
    By Livijn in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2007, 03:32 PM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM