Thread: Quick Screenshot

  1. #1
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341

    Quick Screenshot

    I need a really fast way to take a screenshot and save it in a buffer for use in my program. Could use DirectX to get the main surface or something? If I have the a directdraw object and I lock the primary surface, does the buffer give the entire screen. Or is there a way to get the entire screen.
    Don't quote me on that... ...seriously

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Press "print screen" (for the whole screen) or ALT-printScreen (for the current window).
    Then just read the BMP out of the clipboard within your program.

    > Or is there a way to get the entire screen.
    http://msdn.microsoft.com/library/de...vcons_4esj.asp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I was using:
    Code:
    HDC dc = GetDC(GetDesktopWindow());
    UINT screen_buffer;
    //loop x and y
    screen_buffer[y*scrn_width + x] = GetPixel(dc, x, y);
    But that was way to slow because GDI functions are terrible. I don't know how to read from the clipboard, but if I can't get DirectDraw to give me the entire screen, I'll look it up.
    Don't quote me on that... ...seriously

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Slow?
    Are you planning to make a movie of your desktop or something?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by Salem
    Slow?
    Are you planning to make a movie of your desktop or something?
    Ha ha. No, but good idea. I want to find bitmaps on the screen in real-time. Not to mention that my resolution is 1680 x 1050 so using GetPixel takes like 3 seconds on my machine.
    Don't quote me on that... ...seriously

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should (don't quote me, I'm a real noob at windows programming) be able to create a bitmap from the context, then access the pixels in that much more efficiently than calling getpixel on the context.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Replace my CSafeString instance with a standard C++ string method and this will do what you're after.

    Code:
    HRESULT CGraphicsManager::Screenshot()
    {
    	USES_CONVERSION;
    
    	HRESULT hr;
    	LPDIRECT3DSURFACE9 pTex;
    
    	// create the image surface to store the front buffer image
    	// note that call to GetFrontBuffer will always convert format to A8R8G8B8
    	if (FAILED(hr = v_p3DDevice->CreateOffscreenPlainSurface(800, 600, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &pTex, NULL)))
    		return hr;
    
    	// read the front buffer into the image surface
    	if (FAILED(hr = v_p3DDevice->GetFrontBufferData(0, pTex)))
    	{
    		pTex->Release();
    		return hr;
    	}
    
    	// write the entire surface to the requested file
    	CSafeString file("ScreenShot");
    	file += ntoa(v_ShotCountS);
    	file += ".BMP";
    	hr = D3DXSaveSurfaceToFile( A2W( file() ) , D3DXIFF_BMP, pTex, NULL, NULL);
    
    	// release the image surface
    	pTex->Release();
    
    	v_ShotCountS ++;
    	return hr;
    }
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  8. #8
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by Queatrix
    >> I'm a real noob at windows programming
    PrntScrn is that button on the top right of the keyboard I think.
    Wow. You're definitely not allowed to talk again. First, you misread my original post. I want the screen in a buffer in my program. Pushing the PrntScrn button doesn't do that or I would have already done that. Second, Salem is one of the most respected programmers on here. And that idea that he gave in the post that you quoted is probably going to save me.
    Don't quote me on that... ...seriously

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Correct. Sorry.
    Correct again, Sorry again.
    Last edited by Queatrix; 03-25-2007 at 01:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM