Save screenshot of DirectX window

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Save screenshot of DirectX window

Post by Dude »

I finally found some code that apparently saves DirectX screenshots. Anyone know how to convert it to PureBasic?

From: https://www.gamedev.net/forums/topic/22 ... tx-window/

Code: Select all

Allright, i got it to work finally (working with GDI functions btw):

otherwindow = myClass.Getotherwindowhwnd();
myDC = GetDC (otherwindow);
memDC = CreateCompatibleDC (myDC);
myBitmap = CreateCompatibleBitmap (myDC, 800, 600);
SelectObject (memDC, myBitmap);
rc = BitBlt (memDC, 0, 0, 800, 600, myDC, 0, 0, SRCCOPY);

b = (char*) malloc (800*600*4);
for (int i=0;i < 256; i++) b = i;
BITMAPINFO* myInfo;
myInfo = (BITMAPINFO*) malloc (sizeof (BITMAPINFO) + sizeof(RGBQUAD)*800+600);
myInfo->bmiHeader.biBitCount = 32;
myInfo->bmiHeader.biClrImportant = 0;
myInfo->bmiHeader.biClrUsed = 0;
myInfo->bmiHeader.biCompression = BI_RGB;
myInfo->bmiHeader.biHeight = 600;
myInfo->bmiHeader.biPlanes = 1;
myInfo->bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
myInfo->bmiHeader.biSizeImage = 800*600*4;
myInfo->bmiHeader.biWidth = 800;
myInfo->bmiHeader.biXPelsPerMeter = 1;
myInfo->bmiHeader.biYPelsPerMeter = 1;
GetDIBits (memDC, myBitmap, 0, 600, b, myInfo, DIB_RGB_COLORS);
free (myInfo);
//free (b);

Now i have the screen shot in b[..] and can save it to disk. Thx for all replies anyway!!
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Save screenshot of DirectX window

Post by chi »

You can't use GDI to capture a fullscreen game! It will always just capture the empty window's client area where the game is rendered on, not the game itself...

On Vista or above, start a game in fullscreen and hold [Win] + hit [Tab]. Windows wont be able to show a preview of the game and I bet MS would, if they easily could ;)
The only way I can think of is to hook the game (EndScene, glSwapBuffers, ...) and copy the content of the buffer to an image. Please correct me if I'm wrong...
Et cetera is my worst enemy
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Save screenshot of DirectX window

Post by firace »

A quick, slimmed down conversion of the above code... (just replace TargetWindowTitle with the target you want to capture)
Does that work? (I didn't sleep much - There may be some bugs left)

Code: Select all

otherwindowID = FindWindow_(0,"TargetWindowTitle")

myDC = GetDC_(otherwindowID)
memDC = CreateCompatibleDC_(myDC)
myBitmap = CreateCompatibleBitmap_(myDC, 800, 600)
SelectObject_(memDC, myBitmap)                    
rc = BitBlt_(memDC, 0, 0, 800, 600, myDC, 0, 0, #SRCCOPY)


myInfo.BITMAPINFO
With myInfo\bmiHeader
  \biSize=SizeOf(BITMAPINFOHEADER) ;Size of struct
  \biWidth=800                     ;Bitmap width
  \biHeight=600                    ;Bitmap height
  \biPlanes=1                      ;Single plane
  \biBitCount=32                   ;Bit depth
  \biCompression=#BI_RGB           ;No compression
  \biSizeImage=800*600*4
  \biXPelsPerMeter=0
  \biYPelsPerMeter=0
  \biClrUsed=0
  \biClrImportant=0
EndWith

GetDIBits_(memDC, myBitmap, 0, 600, 0, myInfo, #DIB_RGB_COLORS);

OpenWindow(0, 100, 100, 440, 440, "")
ImageGadget(1,10,10,500,500,myBitmap)
Repeat : Until WaitWindowEvent() = 13116
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Save screenshot of DirectX window

Post by Dude »

chi wrote:start a game in fullscreen and hold [Win] + hit [Tab]. Windows wont be able to show a preview of the game
You're right. :( The Windows thumbnail doesn't show the game. Thanks firace, for converting the code, too. It didn't work, unfortunately.
Post Reply