Page 1 of 1

Dedicated ScreenToImage() function for the screen library

Posted: Tue Mar 23, 2021 9:42 am
by Axeman
Hi. It would be useful if the screen library had a dedicated function for grabbing the displayed screen as an image. This should work with both the windowed and fullscreen versions of the screen. A dedicated function for taking image screenshots could potentially be better optimized than using the drawing library, and may have fewer issues with different graphics subsystems.

I've been using the code below to take screenshots for a windowed screen on Windows PC x64, but on the DirectX11 subsystem the screenshots are corrupted. I ended up having to switch to OpenGL to fix the problem. A dedicated screenshot function might have prevented this issue or made it easier to debug by having a single function with a well defined purpose, rather than messing around with the 2D drawing library.

Code: Select all

DrawPuzzle()
If StartDrawing( ScreenOutput() )

	DrawingMode( #PB_2DDrawing_AllChannels ) ; This is required for the DirectX11 and OpenGL subsytems.
	screenshot = GrabDrawingImage( #PB_Any, 0, 0, ScreenWidth() - 1, ScreenHeight() - 1 )
	StopDrawing()
	
	If screenshot
	
		; Create a unique filename.
		increment = 0
		Repeat
			filepath.s = G_screenshots_folderpath.s + Str( Abs( ElapsedMilliseconds() + increment ) ) + ".jpg"
			increment + 1
		Until FileSize( filepath.s ) = -1
	
		If SaveImage( screenshot, filepath.s, #PB_ImagePlugin_JPEG, 8 )
			Message( "Screenshot was saved to: " + filepath.s )
		Else
			Error( "Screenshot could not be saved to file. File storage may be full." )
		EndIf
		FreeImage( screenshot )
		
	Else
	
		Error( "Unable to create screenshot image. Possibly out of memory." )
		
	EndIf
	
EndIf

Re: Dedicated ScreenToImage() function for the screen librar

Posted: Tue Mar 23, 2021 10:49 am
by BarryG
+1