Thread: Taking a screenshot in DirectDraw

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Taking a screenshot in DirectDraw

    How do I take a screenshot in fullscreen directdraw.

    I hit alt+printscrn, and also only printscrn, but I only get the white window created in the beginning of the program.

    Do I need some technique or (ugh) software?
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code one.

    Simply iterate through your primary surface and write the values to disk - which will be huge in 32-bit color. Another alternative is to use RLE or make a function that will create a BMP out of the data. Then most programs could load your screenshot.

  3. #3
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    whats RLE?
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    This code below uses DirectDraw (DX7) but it's easily enough altered to work with other API's. For instance, for my OpenGL wrapper I pass HDCs directly instead of passing a DX surface and having to grab the HDC from it.

    Inside of "Types.h" I have:
    typedef LPDIRECTDRAWSURFACE7 lpdds7;

    Code:
    #pragma once
    
    #include "Types.h"
    #include <string>
    using std::string;
    
    void SaveScreen(lpdds7 sfHDC);
    PBITMAPINFO MakeBI(lpdds7 sfHDC);
    Code:
    #include "ScreenCapture.h"
    
    HBITMAP NewBmp=NULL, OldBmp=NULL;
    HDC hdcCom=NULL;
    
    void SaveScreen(lpdds7 sfHDC)
    {
    	HANDLE hf;
    	BITMAPFILEHEADER hdr;
    	PBITMAPINFOHEADER pbih;
    	LPBYTE lpBits;
    	DWORD dwTotal;
    	DWORD cb;
    	BYTE *hp;
    	DWORD dwTmp;
    	PBITMAPINFO pbiTest=MakeBI(sfHDC);
    	pbih=(PBITMAPINFOHEADER) pbiTest;
    	lpBits=(LPBYTE)GlobalAlloc(GMEM_FIXED,pbih->biSizeImage);
    	GetDIBits(hdcCom,NewBmp,0,(WORD)pbih->biHeight,lpBits,pbiTest,DIB_RGB_COLORS);
    	//lpwin32_find_data
    	WIN32_FIND_DATA finddata;
    	string sBitmapName;
    	int iFileCheck=1;
    	hf=FindFirstFile("screen*.bmp",&finddata);
    	if(hf==INVALID_HANDLE_VALUE)
    	{
    		sBitmapName="screen0.bmp";
    	}
    	else
    	{
    		while(iFileCheck)
    		{
    			iFileCheck=FindNextFile(hf,&finddata);
    		}
    		string sLastBitmapFound=finddata.cFileName;
    		string sFileID="";
    		char cID[5];
    		int EndOfID, ID, i;
    		for(i=6;i<sLastBitmapFound.size();i++)
    		{
    			if(!isdigit(sLastBitmapFound[i]))
    			{
    				EndOfID=i;
    				break;
    			}
    		}
    		for(i=6;i<EndOfID;i++)
    		{
    			sFileID+=sLastBitmapFound[i];
    		}
    		ID=atoi(sFileID.c_str());
    		ID++;
    		itoa(ID,cID,10);
    		sBitmapName="screen";
    		sBitmapName+=cID;
    		sBitmapName+=".bmp";
    		DebugLog(sBitmapName.c_str(),ID,1);
    	}
    	hf=CreateFile(sBitmapName.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    	hdr.bfType=0x4d42;
    	hdr.bfSize=(DWORD)(sizeof(BITMAPFILEHEADER)+pbih->biSize+pbih->biClrUsed*sizeof(RGBQUAD)+pbih->biSizeImage);
    	hdr.bfReserved1=0;
    	hdr.bfReserved2=0;
    	hdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+pbih->biSize+pbih->biClrUsed*sizeof(RGBQUAD);
    	WriteFile(hf,(LPVOID)&hdr,sizeof(BITMAPFILEHEADER),(LPDWORD)&dwTmp,NULL);
    	WriteFile(hf,(LPVOID)pbih,sizeof(BITMAPINFOHEADER)+pbih->biClrUsed*sizeof(RGBQUAD),(LPDWORD)&dwTmp,NULL);
    	dwTotal=cb=pbih->biSizeImage;
    	hp=lpBits;
    	WriteFile(hf,(LPSTR)hp,(int)cb,(LPDWORD)&dwTmp,NULL);
    	CloseHandle(hf);
    	DebugLog("Scree capture recorded.",0,1);
    	GlobalFree((HGLOBAL)lpBits);
    	SelectObject(hdcCom,OldBmp);
    	DeleteObject(NewBmp);
    	NewBmp=NULL;
    	DeleteDC(hdcCom);
    	hdcCom=NULL;
    }
    
    PBITMAPINFO MakeBI(lpdds7 sfHDC)
    {
    	BITMAP bmp;
    	PBITMAPINFO pbmi;
    	WORD cClrBits;
    	HDC hdc;
    	sfHDC->GetDC(&hdc);
    	NewBmp=CreateCompatibleBitmap(hdc,800,600);
    	hdcCom=CreateCompatibleDC(hdc);
    	OldBmp=(HBITMAP)SelectObject(hdcCom,NewBmp);
    	BitBlt(hdcCom,0,0,800,600,hdc,0,0,SRCCOPY);
    	sfHDC->ReleaseDC(hdc);
    	hdc=NULL;
    	GetObject(NewBmp,sizeof(BITMAP),(LPSTR)&bmp);
    	cClrBits=(WORD)(bmp.bmPlanes*bmp.bmBitsPixel);
    	if(cClrBits==1)
    		cClrBits=1;
    	else if(cClrBits<=4)
    		cClrBits=4;
    	else if(cClrBits<=8)
    		cClrBits=8;
    	else if(cClrBits<=16)
    		cClrBits=16;
    	else if(cClrBits<=24)
    		cClrBits=24;
    	else cClrBits=32;
    	if(cClrBits!=24)
    		pbmi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*(1<<cClrBits));
    	else
    		pbmi=(PBITMAPINFO)LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
    	pbmi->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    	pbmi->bmiHeader.biWidth=bmp.bmWidth;
    	pbmi->bmiHeader.biHeight=bmp.bmHeight;
    	pbmi->bmiHeader.biPlanes=bmp.bmPlanes;
    	pbmi->bmiHeader.biBitCount=bmp.bmBitsPixel;
    	if(cClrBits<24)
    		pbmi->bmiHeader.biClrUsed=(1<<cClrBits);
    	pbmi->bmiHeader.biClrImportant=BI_RGB;
    	pbmi->bmiHeader.biSizeImage=((pbmi->bmiHeader.biWidth*cClrBits+31)&~31)*pbmi->bmiHeader.biHeight;
    	pbmi->bmiHeader.biClrImportant=0;
    	return(pbmi);
    }

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
    By Jareds411 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2004, 05:11 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM