Dedicated ScreenToImage() function for the screen library

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Axeman
User
User
Posts: 89
Joined: Mon Nov 03, 2003 5:34 am

Dedicated ScreenToImage() function for the screen library

Post 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
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Dedicated ScreenToImage() function for the screen librar

Post by BarryG »

+1
Post Reply