Capturing a Screenshot
Posted: 10 Jun 2021, 01:09
I'm currently doing this to capture a screenshot of my application, but I'm not 100% sure that this is the correct way to do it. I see a bunch of methods on RenderContext like Begin/End Render, SetRenderTarget, etc., and it makes me paranoid that my little one-liner is missing some step. What I have works, but if it were doing something bad behind the scenes (big memory leak? failing to release some texture? Forcing Noesis to take extra steps because I skipped something?), I'd have a hard time detecting it.
Here's what I'm doing, please let me know if this is correct.
At startup:
When a screenshot is requested:
That's it, just the one call to CaptureRenderTarget, No BeginRender, no EndRender, nothing. It works, but It's so simple it makes me think I'm missing something.
I assume that the image I get would be the last one that was sent to the screen, that it doesn't have any changes from the last 1/60th of a second. I'm fine with that.
Here's what I'm doing, please let me know if this is correct.
At startup:
Code: Select all
RenderContext mainRenderContext = created from Application.CreateRenderContext();
RenderTarget renderTarget = mainRenderContext.Device.CreateRenderTarget("Screenshot RT", width, height, sampleCount: 1);
Code: Select all
// This is run on the dispatcher thread.
ImageCapture imageCapture = mainRenderContext.CaptureRenderTarget(renderTarget);
SavePngFile(imageCapture.Pixels, imageCapture.Width, imageCapture.Height); // Details of reading the byte[] and saving as PNG not important here...
I assume that the image I get would be the last one that was sent to the screen, that it doesn't have any changes from the last 1/60th of a second. I'm fine with that.