Skip to content
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

Fix the bug of wrong aspect ratio of image cropper in some cases #4482

Closed

Conversation

HHChaos
Copy link
Contributor

@HHChaos HHChaos commented Feb 10, 2022

Fixes #4483 #4478

Fix the issue of incorrect aspect ratio in some cases caused by the timing of updating the layout of the image cropper control.

PR Type

What kind of change does this PR introduce?

What is the current behavior?

What is the new behavior?

PR Checklist

Please check if your PR fulfills the following requirements:

  • Created a feature/dev branch in your fork (vs. submitting directly from a commit on main)
  • Based off latest main branch of toolkit
  • Tested code with current supported SDKs
  • New component
    • Pull Request has been submitted to the documentation repository instructions. Link:
    • Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that)
    • If control, added to Visual Studio Design project
  • Sample in sample app has been added / updated (for bug fixes / features)
  • New major technical changes in the toolkit have or will be added to the Wiki e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

Other information

@ghost
Copy link

ghost commented Feb 10, 2022

Thanks HHChaos for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost requested review from michael-hawker and azchohfi February 10, 2022 09:51
@HHChaos HHChaos changed the title Fix the bug of incorrect aspect ratio in some cases Fix the bug of wrong aspect ratio of image cropper in some cases Feb 10, 2022
@michael-hawker michael-hawker added this to the 8.0 milestone Feb 10, 2022
@michael-hawker michael-hawker added bug 🐛 An unexpected issue that highlights incorrect behavior controls 🎛️ labels Feb 10, 2022
@michael-hawker
Copy link
Member

Thanks @HHChaos! Glad you were able to find a fix. I added an issue for tracking as well.

@jhariel8 want to try it out in your scenario to make sure it's working? You can find out how to use preview packages from https://aka.ms/wct/wiki/previewpackages

@jhariel8
Copy link

@michael-hawker @HHChaos This fix worked perfectly for me! Thanks so much!

Copy link
Member

@michael-hawker michael-hawker left a comment

Choose a reason for hiding this comment

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

Thanks for validating @jhariel8! @Arlodotexe want to review sometime and then we merge this in?

@@ -23,6 +23,12 @@ public partial class ImageCropper
/// <param name="animate">Whether animation is enabled.</param>
private void InitImageLayout(bool animate = false)
{
if (!IsValidRect(CanvasRect))
{
_lazyInitImageLayoutAction = () => InitImageLayout(animate);
Copy link
Member

Choose a reason for hiding this comment

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

Something about this feels off...

@HHChaos were you able to pinpoint exactly what caused the original issue? Could we get some additional insight into the cause and the fix?

Copy link
Member

Choose a reason for hiding this comment

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

I just noticed we don't do any gate on the control to be loaded. Maybe that'd be the better fix is waiting for layout to complete?

Copy link
Contributor Author

@HHChaos HHChaos Mar 2, 2022

Choose a reason for hiding this comment

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

@Arlodotexe The source of this bug is when ImageCropper is in the layout arranging phase, it may take more time if the available sizes are infinite, and because CanvasRect is not a valid size before the layout is completed, this bug may occur if the InitImageLayout method is called at this time.

@michael-hawker I have tested it, when loaded occurs, CanvasRect may still be an invalid value.

Copy link
Member

Choose a reason for hiding this comment

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

This fix feels like it's patching over a slightly deeper issue.

Looking at the code, it appears that

  • We're relying on the size of the PART_ImageCanvas that contains the image, rather than the image itself.
  • We're calling InitImageLayout when the source has changed, but not when the image is loaded.

If the above ends up being the cause of other issues down the road, we don't want to be band-aid patching them. When underlying issues get fixed that have been partially patched over, we need to clean up all those patches, and there are no notes or comments to tell us what code should be included in the cleanup.

To save time and headache later, I think we should look at fixing this by fixing the underlying problem.

Copy link
Member

@Arlodotexe Arlodotexe Aug 10, 2022

Choose a reason for hiding this comment

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

Looks like the source of the problem is that calling await imgCropper.LoadImageFromFile(file); doesn't wait for the image to load before completing.

var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets\\Screenshot 2021-03-16 083652.png"));
await imgCropper.LoadImageFromFile(file);
this.imgCropper.AspectRatio = 1.0;

Adding a Task.Delay() with any value (minimum of 1) fixes the issue.

Notably, this issue also exists when do you

imgCropper.Source = writableBitmap;
imgCropper.AspectRatio = 1;

Seems this scenario wasn't considered when designing the architecture for this control. I'll see if there's a better fix for this than lazy-loading. A few options come to mind...

Copy link
Member

@Arlodotexe Arlodotexe Aug 10, 2022

Choose a reason for hiding this comment

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

Moving additional findings to here in case I need to open a new PR for this.

Copy link
Member

Choose a reason for hiding this comment

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

PR #4720 opened with a much simpler fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior controls 🎛️ hotfix 🌶
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to get perfect square with ImageCropper
4 participants