How can I load a screenshot of the desktop into my HBITMAP var?

This is a discussion on How can I load a screenshot of the desktop into my HBITMAP var? within the Windows Programming forums, part of the Platform Specific Boards category; How can I load a screenshot of the desktop into my HBITMAP var? Thanks in advance!...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question How can I load a screenshot of the desktop into my HBITMAP var?

    How can I load a screenshot of the desktop into my HBITMAP var?

    Thanks in advance!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Big thanks to anonytmouse for flat out giving me this awhile back :

    Code:
    void Screenshot(HDC hDC, HWND hwnd)
    {
    	/* Get the size of the screen */
    	SIZE size; 
    	size.cx = GetSystemMetrics(SM_CXSCREEN);
    	size.cy = GetSystemMetrics(SM_CYSCREEN);
    
    	/* Get a device context for the screen */
    	HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
    
    	/* Create a memory bitmap and put it in a memory DC so we can draw on it */
    	HDC hdcShot = CreateCompatibleDC(hdcScreen);
    	HBITMAP hBitmapShot = CreateCompatibleBitmap(hdcScreen, size.cx, size.cy);
    	HBITMAP hBitmapOld = (HBITMAP) SelectObject(hdcShot, hBitmapShot);
    
    	/* Copy the screen image to our memory bitmap */
    	BitBlt(hdcShot, 0, 0, size.cx, size.cy, hdcScreen, 0, 0, SRCCOPY);
    
    	/* Save our memory bitmap to file */
    	Save(hwnd, CreateInfoHeader(hwnd, hBitmapShot), hBitmapShot, hdcShot);
    
    	/* Cleanup memory bitmap and DC */
    	SelectObject(hdcShot, hBitmapOld);
    	DeleteObject(hBitmapShot);
    	DeleteDC(hdcShot);
    
    	/* Cleanup screen DC */
    	DeleteDC(hdcScreen);
    }

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thanks, anonytmouse and tonto.

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I'm sorry I don't understand something here, what is the HDC hDC passed for if it's not even used? And what is the filename?

    Can someone explain why are there so many compatibale DCs?
    Last edited by Devil Panther; 10-01-2005 at 11:32 AM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thats a good question about that "hDC" var.

    Now I've got a problem.

    Code:
    void Screenshot(HWND hwnd)
    {
     /* Get the size of the screen */
     SIZE size; 
     size.cx = GetSystemMetrics(SM_CXSCREEN);
     size.cy = GetSystemMetrics(SM_CYSCREEN);
     /* Get a device context for the screen */
     HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
     /* Create a memory bitmap and put it in a memory DC so we can draw on it */
     HDC hdcShot = CreateCompatibleDC(hdcScreen);
     HBITMAP hBitmapShot = CreateCompatibleBitmap(hdcScreen, size.cx, size.cy);
     HBITMAP hBitmapOld = (HBITMAP) SelectObject(hdcShot, hBitmapShot);
    	
    	/* Copy the screen image to our memory bitmap */
     BitBlt(hdcShot, 0, 0, size.cx, size.cy, hdcScreen, 0, 0, SRCCOPY);
     /* Save our memory bitmap to file */
     //Save(hwnd, CreateInfoHeader(hwnd, hBitmapShot), hBitmapShot, hdcShot);
     /* Cleanup memory bitmap and DC */
     SelectObject(hdcShot, hBitmapOld);
     DeleteObject(hBitmapShot);
     DeleteDC(hdcShot);
     /* Cleanup screen DC */
     DeleteDC(hdcScreen);
     
    	  HDC hdc = GetDC(hwnd);
    	  HDC hdcMemory;
    	  hdcMemory = CreateCompatibleDC(hdc);
    	  SelectObject(hdcMemory, hBitmapShot);
    	  BitBlt(hdc, 0, 0, size.cx, size.cy, hdcMemory, 0, 0, SRCCOPY);
    	  SelectObject(hdcMemory, hBitmapOld);
    	  BitBlt(hdc, 0, 0, size.cx, size.cy, hdcMemory, 0, 0, SRCCOPY);
    	  DeleteDC(hdcMemory); 
    	  Beep(2000,100);
    }
    I call this function, and use my windows HWND as the argument. But it doesn't print anything at all.

    What's going on?

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Nevermind. I figured it out!

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    let me guess you need the handle of the desktop, right?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Let me check...


    Doesn't look like it.

    Even if it did, HWND_DESKTOP works.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Cool-August
    Nevermind. I figured it out!
    In that case what was the problem
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I get CreateInfoHeader undeclared error...

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Save and CreateInfoHeader are custom functions for saving a bitmap to file. Tonto may provide them or you can use this MSDN code for storing a bitmap instead.

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by Devil Panther
    In that case what was the problem
    The same problem maxorator had, so I decided to search google's beta groups.

  13. #13
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    Quote Originally Posted by Devil Panther
    I'm sorry I don't understand something here, what is the HDC hDC passed for if it's not even used? And what is the filename?

    Can someone explain why are there so many compatibale DCs?

    HDC is the Direct Context which is used in win32 to write anything to the screen ex. lines or GFX anything like that or simply managing Bitmaps OpenGL uses HDC
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help assignment
    By 6kaine9 in forum C Programming
    Replies: 26
    Last Post: 10-19-2008, 08:51 PM
  2. drawing on bitmaps
    By eth0 in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2006, 04:56 PM
  3. Just finished assembly assignment
    By xddxogm3 in forum Tech Board
    Replies: 6
    Last Post: 10-08-2005, 04:01 PM
  4. server question
    By xddxogm3 in forum Tech Board
    Replies: 24
    Last Post: 01-21-2004, 12:12 AM
  5. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21