Garry's Mod Wiki

render.Capture

  string render.Capture( table captureData )

Description

Captures a part of the current render target and returns the data as a binary string in the given format.

Since the pixel buffer clears itself every frame, this will return a black screen outside of render hooks. To capture the user's final view, use GM:PostRender. This will not capture the Steam overlay or third-party injections (such as the Discord overlay, Overwolf, and advanced cheats) on the user's screen.

In PNG mode, this function can produce unexpected result where foreground is rendered as transparent. This is caused by render.SetWriteDepthToDestAlpha set to true when doing most of render operations, including rendering in _rt_fullframefb. If you want to capture render target's content as PNG image only for output quality, set RenderCaptureData structure's alpha to false when capturing render targets with render.SetWriteDepthToDestAlpha set to true.

Issue Tracker: 2571
This function will return nil if escape menu is open

Arguments

1 table captureData
Parameters of the capture. See RenderCaptureData structure.

Returns

1 string
binaryData

Example

How you could use this to save a picture of your screen.

local ScreenshotRequested = false function RequestAScreenshot() gui.HideGameUI() ScreenshotRequested = true end -- For the sake of this example, we use a console command to request a screenshot concommand.Add( "make_screenshot", RequestAScreenshot ) hook.Add( "PostRender", "example_screenshot", function() if ( !ScreenshotRequested ) then return end ScreenshotRequested = false local data = render.Capture( { format = "png", x = 0, y = 0, w = ScrW(), h = ScrH(), alpha = false -- only needed for the png format to prevent the depth buffer leaking in, see BUG } ) file.Write( "image.png", data ) end )
Output: You should now have image.png in your garrysmod/data folder, containing a screenshot.