Skip to content

Add JPEG format support in take screenshot#187

Merged
nowsprinting merged 4 commits intomasterfrom
feature/jpeg
Mar 1, 2026
Merged

Add JPEG format support in take screenshot#187
nowsprinting merged 4 commits intomasterfrom
feature/jpeg

Conversation

@nowsprinting
Copy link
Owner

@nowsprinting nowsprinting commented Feb 28, 2026

Summary

Adds JPEG encoding support to the async screenshot API (TakeScreenshotAsync and related methods).

Changes

New API

  • TakeScreenshotAsJpegBytesAsync(float scale, int quality) — captures and returns a JPEG-encoded byte array with configurable quality (1–100, default 75)
  • TakeScreenshotAsync — new format and quality parameters for JPEG output; file extension is automatically set to .jpeg when format: ImageFormat.Jpeg

Refactoring

  • Extracted shared pixel capture logic into CaptureScreenPixelsAsync, used by both TakeScreenshotAsPngBytesAsync and the new TakeScreenshotAsJpegBytesAsync
  • Added GetFileExtension(ImageFormat) helper to resolve format-dependent file extensions
  • Updated GetSavePath to accept an explicit extension argument (previously hardcoded to "png")

Fixes

  • Fixed XML doc for the filename parameter of TakeScreenshotAsync: the default filename extension now correctly reflects the format parameter instead of hardcoding .png
  • Changed format.ToString().ToLower() to ToLowerInvariant() in GetFileExtension to avoid locale-dependent behavior for file extensions

Tests

  • TakeScreenshotAsJpegBytesAsync_ReturnsJpegBytes — verifies JPEG output dimensions
  • TakeScreenshotAsync_WithJpegFormat_SavesJpegFile — parameterized over quality values [1, 50, 100]
  • TakeScreenshotAsync_WithJpegFormatAndFilename_SavesWithJpegExtension — verifies .jpeg extension is appended when omitted
  • TakeScreenshotAsync_WithJpegFormatAndJpegFilename_DoesNotDuplicateExtension — verifies no .jpeg.jpeg double extension
  • TakeScreenshotAsync_WithJpegFormat_SaveToDefaultPath — verifies default save path uses .jpeg extension when no filename is provided

Notes

  • The coroutine API (TakeScreenshot) and TakeScreenshotAttribute is unchanged and continues to save PNG only
  • When SystemInfo.supportsAsyncGPUReadback is false, the scale parameter is ignored in the fallback path (pre-existing behavior, tracked separately)

nowsprinting and others added 3 commits March 1, 2026 04:06
Add ImageFormat enum (Png/Jpeg), TakeScreenshotAsJpegBytesAsync stub,
extend TakeScreenshotAsync with format/quality params, and update
GetSavePath to accept extension as parameter.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Use AsyncGPUReadback path with ImageConversion.EncodeArrayToJPG when
supported, falling back to EncodeToJPG for other platforms.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…eScreenPixelsAsync

Deduplicate ~35 lines of identical screen capture logic (camera setup,
EndOfFrameAsync, RenderTexture capture, GPU readback, thread switching)
shared between TakeScreenshotAsPngBytesAsync and TakeScreenshotAsJpegBytesAsync.
Replace fragile format.ToString().ToLower() with an explicit GetFileExtension switch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 28, 2026 20:20
@github-actions github-actions bot added the enhancement New feature or request label Feb 28, 2026
@nowsprinting nowsprinting changed the title Add JPEG screenshot support Add JPEG format support in take screenshot Feb 28, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds JPEG encoding support to the runtime ScreenshotHelper API (primarily for the async/Awaitable screenshot path) and expands the test suite to cover JPEG output.

Changes:

  • Added ImageFormat enum to represent screenshot encoding formats (PNG/JPEG).
  • Extended ScreenshotHelper.TakeScreenshotAsync to support JPEG output (with configurable JPEG quality) and choose the correct output extension.
  • Added new runtime tests for JPEG byte capture and JPEG file output/extension behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
Tests/RuntimeInternals/ScreenshotHelperTest.cs Adds tests for JPEG byte output and JPEG file saving/extension behavior.
RuntimeInternals/ScreenshotHelper.cs Implements JPEG encoding path, refactors pixel capture to share between PNG/JPEG, and updates save-path extension handling.
RuntimeInternals/ImageFormat.cs Introduces ImageFormat enum (Png/Jpeg) used by the screenshot API.
RuntimeInternals/ImageFormat.cs.meta Unity meta file for the new ImageFormat.cs asset.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

This comment has been minimized.

- Fix XML doc for `filename` param of `TakeScreenshotAsync`: extension
  depends on `format` parameter, not always ".png"
- Use `ToLowerInvariant()` instead of `ToLower()` in `GetFileExtension`
  to avoid locale-dependent behavior for file extensions
- Add `TakeScreenshotAsync_WithJpegFormat_SaveToDefaultPath` test to
  cover the case where filename is omitted with JPEG format

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link

github-actions bot commented Mar 1, 2026

Code Metrics Report

master (8806ff3) #187 (4531665) +/-
Coverage 80.2% 80.4% +0.1%
Code to Test Ratio 1:1.0 1:1.0 +0.0
Test Execution Time 5m47s 5m44s -3s
Details
  |                     | master (8806ff3) | #187 (4531665) |  +/-  |
  |---------------------|------------------|----------------|-------|
+ | Coverage            |            80.2% |          80.4% | +0.1% |
  |   Files             |               45 |             45 |     0 |
  |   Lines             |             1663 |           1695 |   +32 |
+ |   Covered           |             1335 |           1363 |   +28 |
+ | Code to Test Ratio  |            1:1.0 |          1:1.0 |  +0.0 |
  |   Code              |             2917 |           2979 |   +62 |
+ |   Test              |             3077 |           3149 |   +72 |
+ | Test Execution Time |            5m47s |          5m44s |   -3s |

Code coverage of files in pull request scope (90.6% → 89.8%)

Files Coverage +/- Status
RuntimeInternals/ScreenshotHelper.cs 89.8% -0.9% modified

Reported by octocov

@nowsprinting nowsprinting merged commit 4744782 into master Mar 1, 2026
11 checks passed
@nowsprinting nowsprinting deleted the feature/jpeg branch March 1, 2026 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants