Fix GH-23459: Improve imagegrabscreen() and imagegrabwindow() performance on Windows - #23485
Open
LamentXU123 wants to merge 1 commit into
Open
Fix GH-23459: Improve imagegrabscreen() and imagegrabwindow() performance on Windows#23485LamentXU123 wants to merge 1 commit into
LamentXU123 wants to merge 1 commit into
Conversation
Member
|
Not out of bounds from what I see ... The y <= Height / x <= Width bound is a genuine off-by-one and should have been <, but neither side of the loop body touches memory out of bounds: if you think I m wrong please add a test. |
Member
Author
|
Blerg I made a typo here. Yeah what I meant is off-by-one :) |
staabm
reviewed
Aug 28, 2026
Member
|
nope. already fixed and different issue anyway (as in its fix can t apply here). |
Member
Author
|
I think it is reasonable to document the optimization in #23457 in the UPGRADING file. But that's a different story. I will take care of this in a different commit. |
devnexen
approved these changes
Aug 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The original implementation of
imagegrabscreenis super weird. It currently callsGetPixel()once for every pixel after capturing the desktop (???). On a 1560x1040 display, this results in more than 1.6 million GDI calls and takes around 2.8 seconds locally. (?????) Besides, I find aout-of-boundoff-by-one read in the original implementation. This is also true forimagegrabwindow. They share the same exact logic and don't even make this out as a shared helper...This is a easy fix. Anyways, now we retrieve the bitmap in a single
GetDIBits()call as a top-down 32-bit DIB, then convert the returned RGBQUAD buffer directly into the GD true-color image.I run the benchmark locally with a 1560×1040 pic.
Before: 2.997434s
After: 0.003682s
A 814x optimization in total. Fixes #23459