Fix ImageCropper returning a crop backed by a disposed stream (breaking: crop is no longer JPEG) - #1530
Draft
imnasnainaec wants to merge 5 commits into
Draft
Fix ImageCropper returning a crop backed by a disposed stream (breaking: crop is no longer JPEG)#1530imnasnainaec wants to merge 5 commits into
imnasnainaec wants to merge 5 commits into
Conversation
Return the cropped bitmap as a stand-alone copy instead of round-tripping through a MemoryStream that had to outlive it; unsubscribe the disposed Application.Idle handler; dispose and null out prior image state before re-assignment; and fix a height-vs-width typo that skipped downscaling of tall images. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Contributor
Palaso Tests 4 files ±0 4 suites ±0 10m 10s ⏱️ -23s Results for commit 6e07840. ± Comparison against base commit a8bc5a7. This pull request removes 1 and adds 4 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
ShowToolboxWith_PreExisting_EnsureRawFormatUnchanged asserted that cropping preserves the original RawFormat, which no longer holds now that GetCroppedImage intentionally returns a stand-alone bitmap. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This comment was marked as outdated.
This comment was marked as outdated.
imnasnainaec
marked this pull request as draft
July 13, 2026 16:36
…ea fix (BL-1275) Strengthen the double-dispose and reassignment tests to actually exercise and verify cleanup (temp file and cropping image disposal) instead of only asserting no exception is thrown, and add a regression test for the height/width downscaling typo. Add the missing CHANGELOG entry for the null-guard in CalculateSourceImageArea, and trim stale comments in GetCroppedImage describing the removed JPEG re-encoding approach. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 task
This was referenced Aug 26, 2026
The Image setter's dispose-and-null block and the CalculateSourceImageArea null guard it required are removed here, leaving this branch to the GetCroppedImage change alone.
Contributor
Author
|
@andrew-polk What's your take on the breaking change of this pr? (Don't worry about doing code review... not worthwhile unless and until we decide it's a worthwhile change.) |
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.
Warning
Breaking change.
GetCroppedImageno longer returns a JPEG-encoded bitmap for JPEG sources; it returns a stand-alone bitmap whoseRawFormatisMemoryBmp. Callers that readRawFormat, or that callImage.Save(path)on the result and rely on GDI+ picking the JPEG encoder implicitly, must now pass an explicitImageFormat— a.jpgpath would otherwise receive PNG bytes. Going throughPalasoImage.Save(path)is unaffected, since it picks the encoder from the file extension. ImageToolbox's own use goes throughPalasoImage, so nothing in this repo changes behaviour.GetCroppedImagere-encodes a JPEG crop through aMemoryStreamand returns whatImage.FromStreamproduces. That stream sits in ausing, so it is disposed before the bitmap is returned, and GDI+ decodes lazily — the crop refers to a stream that is already gone. The next thing to touch its pixels fails with "A generic error occurred in GDI+".That is #1275: choose an image from a file, Crop, Choose, Crop. Leaving the Crop tab calls
GetImage, which stores the stream-backed crop on thePalasoImage; returning re-enters theImagesetter, which callsvalue.Image.Save(...)on that crop and throws. Only JPEG sources take the re-encode path, matching the report's "happens for most but not all".The change
Return a stand-alone
new Bitmap(cropped)instead of the re-encoded one, so nothing holds a lazy reference to a stream or a file. Preserving the JPEGRawFormatis what required the re-encode in the first place, so giving it up is what makes the fix possible — hence the breaking change above.With the re-encode gone
_originalFormathas no remaining use, so it goes too, along with theRequire.Thatself-check, theSIL.Codeimport, and the comments describing the old workaround.Removing
_originalFormatfixes a second bug on the way. It was assigned only inSetImage, so assigning theImageproperty directly left it null, andGetCroppedImagethrew aNullReferenceExceptionreading_originalFormat.Guid— swallowed by the catch and surfaced as "Sorry, there was a problem getting the image".ShowToolboxWith_PreExisting_EnsureRawFormatUnchangedis deleted: it is[Explicit("By hand only")]so it never ran in CI, and it asserted exactly the JPEGRawFormatthis change gives up.Tests
Three of the four added tests fail without the change:
GetCroppedImage_JpegImage_ReturnsUsableBitmap— re-encoding the returned crop throws.GetCroppedImage_ImageSetViaPropertyDirectly_ReturnsUsableBitmap— the null_originalFormatNRE.GetImage_ReCropPreviouslyCroppedJpeg_DoesNotThrow— the Crash in ImageToolbox switching between Crop and Choose #1275 round trip.GetCroppedImage_PngImage_ReturnsUsableBitmappasses without the change too, since PNG never took the re-encode path; it is there so both formats are covered symmetrically.--filter ImageToolboxis green on net8.0-windows and net48: 49 passed, 1 pre-existing skip.Fixes #1275
This change is