-
-
Notifications
You must be signed in to change notification settings - Fork 49
Fix ImageCropper crash when switching Crop/Choose tabs #1521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
imnasnainaec
wants to merge
23
commits into
master
Choose a base branch
from
fix/1275-imagecropper-crash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d9b5d24
Fix ImageCropper Application.Idle leak and premature MemoryStream dis…
imnasnainaec aec870b
Remove issue number from CHANGELOG entry
imnasnainaec 2123b9d
Move Application.Idle unsubscription before try-catch in Dispose
imnasnainaec 943dd7e
Use direct cast instead of as-cast for Image.FromStream result
imnasnainaec 69d3f35
Dispose MemoryStream on exception path in GetCroppedImage
imnasnainaec 9b13531
Fix CHANGELOG encoding corruption
imnasnainaec a59dd66
Fix Image setter leaking previous _savedOriginalImage and _croppingImage
imnasnainaec a5355d6
Update CHANGELOG to include Image setter leak fix
imnasnainaec 09796e3
Null fields after disposal; fix height vs width typo in Image setter
imnasnainaec 6da53b8
Update CHANGELOG for null-after-dispose and height typo fixes
imnasnainaec 845054a
Guard CalculateSourceImageArea against null _croppingImage
imnasnainaec 455cb6c
Split CHANGELOG entry into independent items
imnasnainaec 0686d4d
Set _originalFormat in Image setter, not only in SetImage
imnasnainaec 0bd93d2
Add CHANGELOG entry for _originalFormat fix
imnasnainaec 388e01b
Add tests for direct Image property set and SetImage re-set
imnasnainaec c6f1c7c
Remove redundant _originalFormat assignment from SetImage
imnasnainaec fd7c24d
Remove redundant test assertions
imnasnainaec ffc88b1
Improve assertion symmetry in ImageCropper tests
imnasnainaec 969b09b
Rename SetImage test to reflect its actual assertion
imnasnainaec c2fb3c6
Merge branch 'master' into fix/1275-imagecropper-crash
imnasnainaec f9e51c5
Add re-crop regression test; strengthen JPEG/PNG cropper tests
imnasnainaec ce6c57a
Trim test comments in ImageCropperTests
imnasnainaec 6843326
Dispose cropped bitmap if JPEG re-encode fails in GetCroppedImage
imnasnainaec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
171 changes: 171 additions & 0 deletions
171
SIL.Windows.Forms.Tests/ImageToolbox/ImageCropperTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using NUnit.Framework; | ||
| using SIL.IO; | ||
| using SIL.Windows.Forms.ImageToolbox; | ||
| using SIL.Windows.Forms.ImageToolbox.Cropping; | ||
|
|
||
| namespace SIL.Windows.Forms.Tests.ImageToolbox | ||
| { | ||
| [Apartment(ApartmentState.STA)] | ||
| [TestFixture] | ||
| public class ImageCropperTests | ||
| { | ||
| [Test] | ||
| public void Dispose_CalledTwice_DoesNotThrow() | ||
| { | ||
| var cropper = new ImageCropper(); | ||
| Assert.DoesNotThrow(() => cropper.Dispose()); | ||
| Assert.DoesNotThrow(() => cropper.Dispose()); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetCroppedImage_PngImage_ReturnsUsableBitmap() | ||
| { | ||
| using (var tempFile = TempFile.WithExtension(".png")) | ||
| { | ||
| using (var bmp = new Bitmap(100, 80)) | ||
| bmp.Save(tempFile.Path, ImageFormat.Png); | ||
|
|
||
| using (var palasoImage = PalasoImage.FromFile(tempFile.Path)) | ||
| using (var cropper = new ImageCropper()) | ||
| { | ||
| cropper.Size = new Size(400, 300); | ||
| cropper.SetImage(palasoImage); | ||
|
|
||
| using (var result = cropper.GetCroppedImage()) | ||
| { | ||
| Assert.IsNotNull(result); | ||
| Assert.Greater(result.Width, 0); | ||
| // Re-encode to force GDI+ to re-read the pixel data from the backing store. | ||
| using (var ms = new MemoryStream()) | ||
| Assert.DoesNotThrow(() => result.Save(ms, ImageFormat.Png)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetCroppedImage_JpegImage_ReturnsJpegBitmapUsableAfterReturn() | ||
| { | ||
| using (var tempFile = TempFile.WithExtension(".jpg")) | ||
| { | ||
| using (var bmp = new Bitmap(100, 80)) | ||
| bmp.Save(tempFile.Path, ImageFormat.Jpeg); | ||
|
|
||
| using (var palasoImage = PalasoImage.FromFile(tempFile.Path)) | ||
| using (var cropper = new ImageCropper()) | ||
| { | ||
| cropper.Size = new Size(400, 300); | ||
| cropper.SetImage(palasoImage); | ||
|
|
||
| Image result; | ||
| // GetCroppedImage returns a Bitmap backed by a MemoryStream for JPEG. | ||
| // The stream must still be alive after the method returns. | ||
| result = cropper.GetCroppedImage(); | ||
| try | ||
| { | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(ImageFormat.Jpeg.Guid, result.RawFormat.Guid); | ||
| // Re-encode to force GDI+ to re-read the pixel data from the backing stream. | ||
| using (var ms = new MemoryStream()) | ||
| Assert.DoesNotThrow(() => result.Save(ms, ImageFormat.Png)); | ||
| } | ||
| finally | ||
| { | ||
| result?.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| [Test] | ||
| public void GetCroppedImage_JpegImage_SetViaPropertyDirectly_ReturnsJpegBitmap() | ||
| { | ||
| // Setting Image directly (not via SetImage) must still initialize _originalFormat so | ||
| // GetCroppedImage does not throw NullReferenceException on JPEG re-encoding. | ||
| using (var tempFile = TempFile.WithExtension(".jpg")) | ||
| { | ||
| using (var bmp = new Bitmap(100, 80)) | ||
| bmp.Save(tempFile.Path, ImageFormat.Jpeg); | ||
|
|
||
| using (var palasoImage = PalasoImage.FromFile(tempFile.Path)) | ||
| using (var cropper = new ImageCropper()) | ||
| { | ||
| cropper.Size = new Size(400, 300); | ||
| cropper.Image = palasoImage; // bypass SetImage intentionally | ||
|
|
||
| using (var result = cropper.GetCroppedImage()) | ||
| { | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(ImageFormat.Jpeg.Guid, result.RawFormat.Guid); | ||
| Assert.Greater(result.Width, 0); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void SetImage_Reassign_UpdatesFormatAndDoesNotThrow() | ||
| { | ||
| using (var tempFile1 = TempFile.WithExtension(".png")) | ||
| using (var tempFile2 = TempFile.WithExtension(".jpg")) | ||
| { | ||
| using (var bmp = new Bitmap(100, 80)) | ||
| { | ||
| bmp.Save(tempFile1.Path, ImageFormat.Png); | ||
| bmp.Save(tempFile2.Path, ImageFormat.Jpeg); | ||
| } | ||
|
|
||
| using (var img1 = PalasoImage.FromFile(tempFile1.Path)) | ||
| using (var img2 = PalasoImage.FromFile(tempFile2.Path)) | ||
| using (var cropper = new ImageCropper()) | ||
| { | ||
| cropper.Size = new Size(400, 300); | ||
| cropper.SetImage(img1); | ||
| cropper.SetImage(img2); | ||
|
|
||
| using (var result = cropper.GetCroppedImage()) | ||
| { | ||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(ImageFormat.Jpeg.Guid, result.RawFormat.Guid); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void SetImage_ReCropPreviouslyCroppedJpeg_DoesNotThrow() | ||
| { | ||
| // GetImage() returns a JPEG Bitmap backed by a MemoryStream; feeding that result | ||
| // back into a new cropper re-saves it in the Image setter (value.Image.Save), which | ||
| // crashed once the backing stream had been disposed. | ||
| using (var tempFile = TempFile.WithExtension(".jpg")) | ||
| { | ||
| using (var bmp = new Bitmap(1200, 900)) | ||
| bmp.Save(tempFile.Path, ImageFormat.Jpeg); | ||
|
|
||
| // GetImage() returns the same PalasoImage instance, now holding the cropped JPEG, | ||
| // so the outer using disposes it exactly once. | ||
| using (var palasoImage = PalasoImage.FromFile(tempFile.Path)) | ||
| { | ||
| PalasoImage cropped; | ||
| using (var cropper1 = new ImageCropper()) | ||
| { | ||
| cropper1.Size = new Size(400, 300); | ||
| cropper1.SetImage(palasoImage); | ||
| cropped = cropper1.GetImage(); | ||
| } | ||
| Assert.That(cropped, Is.Not.Null); | ||
|
|
||
| using (var cropper2 = new ImageCropper()) | ||
| { | ||
| cropper2.Size = new Size(400, 300); | ||
| Assert.DoesNotThrow(() => cropper2.SetImage(cropped)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.