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

WPF Hybrid: change copper src image old image #309

Closed
LeeCY-C opened this issue Mar 8, 2024 · 10 comments
Closed

WPF Hybrid: change copper src image old image #309

LeeCY-C opened this issue Mar 8, 2024 · 10 comments
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested

Comments

@LeeCY-C
Copy link

LeeCY-C commented Mar 8, 2024

Describe the bug
Uploading 8d4df8b72b62900b1e1c55786d41bcd.png…

load default image and first change image is no problem.
The problem arise when change the image second time .
Change copper src image old image ,but old image append under the new image.
This problem will not always arise,It just happens sometimes.

@MaxymGorn
Copy link
Member

MaxymGorn commented Mar 8, 2024

Hi @LeeCY-C you should use 'Replace' or 'Change' functionality instead of changing src image: https://cropperblazor.github.io/examples/replacing#simple-usage, https://cropperblazor.github.io/examples/rebuild#simple-usage. In addition, you can see example of code from demo site:

public async Task InputFileChangeAsync(InputFileChangeEventArgs inputFileChangeEventArgs)
,
public async Task ReplaceImageAsync(InputFileChangeEventArgs inputFileChangeEventArgs)

@MaxymGorn MaxymGorn added documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested labels Mar 8, 2024
@MaxymGorn MaxymGorn self-assigned this Mar 8, 2024
@LeeCY-C
Copy link
Author

LeeCY-C commented Mar 10, 2024

8d4df8b72b62900b1e1c55786d41bcd this is the bug image

@LeeCY-C
Copy link
Author

LeeCY-C commented Mar 11, 2024

This is my change image code.
` public async Task FileInputValueChanged(IBrowserFile imageFile)
{
try
{
if (imageFile != null)
{
string oldSrc = imageSrc;
string newSrc = await ImageCropper!.GetImageUsingStreamingAsync(imageFile, imageFile.Size);

                if (IsErrorLoadImage)
                {
                    IsAvailableInitCropper = true;
                    IsErrorLoadImage = false;
                }
                else
                {
                    IsAvailableInitCropper = false;
                }

                await Task.WhenAll(
                    ImageCropper?.ReplaceAsync(newSrc, false).AsTask(),
                    ImageCropper?.RevokeObjectUrlAsync(oldSrc).AsTask())
                    .ContinueWith(x =>
                    {
                        imageSrc = newSrc;
                    });
            }
        }
        catch (Exception exception)
        {
            await JSRuntime!.InvokeVoidAsync("console.log", $"{exception.Message}");
        }
    }`

@MaxymGorn
Copy link
Member

MaxymGorn commented Mar 11, 2024

@LeeCY-C I memorize in old versions library we have that issue. Could you check your version of library or try to update and use newest version? In addition, could you provide temporary project with reproduce of bug to check it? Moreover, verify cropper component parameters: IsAvailableInitCropper, IsErrorLoadImage and so on... Maybe, you can find the answer this following conversation: #150

@MaxymGorn
Copy link
Member

MaxymGorn commented Mar 11, 2024

Moreover, in your case we expecting to double initialization of cropper component, but we expect that regarding specific of work cropper component in our docs
Screenshot_20240311_234856_Chrome

You can governance of cropper lifestyle independently, just using destroy or init function of cropper, but it should works in newest version of library properly.

@LeeCY-C
Copy link
Author

LeeCY-C commented Mar 12, 2024

CopperTestFor#309.zip
This is an temporary project, and can recurrent this bug.

@MaxymGorn
Copy link
Member

MaxymGorn commented Mar 12, 2024

Hi @LeeCY-C in your case you should set up IsAvailableInitCropper = false; due to cropper.js rebuild inner cropper when you passed hasSameSize with false value in ReplaceAsync method.
image
Seems we have this condition in our associated docs page, but you don't use error handing from processing images in library:

image

@MaxymGorn
Copy link
Member

MaxymGorn commented Mar 12, 2024

@LeeCY-C sometimes in your example project I have the same error for specific images, I have no clue in this moment why it's happened, I suppose it's a problem with hybrid WebView in WPF. I'm would investigate about this issue in multiple platforms. In your case, I'm recommended try to use rebuild cropper functionality: https://cropperblazor.github.io/examples/rebuild#simple-usage.

Code:

public async Task FileInputValueChanged(IBrowserFile imageFile)
{
    try
    {
        if (imageFile != null)
        {
            string oldSrc = imageSrc;
            imageSrc = await ImageCropper!.GetImageUsingStreamingAsync(imageFile, imageFile.Size);

            IsAvailableInitCropper = true;
            IsErrorLoadImage = false;

            ImageCropper!.Destroy();
            ImageCropper!.RevokeObjectUrlAsync(oldSrc);
        }
    }
    catch (Exception exception)
    {
        _snackbarOptions.Content = exception.Message;
        PopupService!.EnqueueSnackbarAsync(_snackbarOptions);
    }
}

Although you will have the same result. In addition, you need to add a style so that during processing (loading) the image is not be too large during processing in the cropper when changing the image

@MaxymGorn MaxymGorn added the bug Something isn't working label Mar 12, 2024
@LeeCY-C
Copy link
Author

LeeCY-C commented Mar 13, 2024

Thank you for your patient guidance. Now it work well.

@LeeCY-C LeeCY-C closed this as completed Mar 14, 2024
@MaxymGorn
Copy link
Member

MaxymGorn commented Mar 15, 2024

Hi @LeeCY-C . I'm figure out why it's not working correctly in hybrid with WPF .net6 based on WebView2. I think it's a very specific problem in this platform, because we don't test on this platform and use Cropper.Blazor library. Overall, I see what caused this problem:
image
I see await ImageCropper!.RevokeObjectUrlAsync(oldSrc).AsTask(); not works properly sometimes in WebView with WPF platform. As result, we have the second cropper in this case. I'm resolve it with following code:

string OldSrc = string.Empty;

public async Task FileInputValueChanged(IBrowserFile imageFile)
{
    try
    {
        if (imageFile != null)
        {
            OldSrc = imageSrc;
            string newSrc = await ImageCropper!.GetImageUsingStreamingAsync(imageFile, imageFile.Size);

            IsAvailableInitCropper = false;

            await ImageCropper!.ReplaceAsync(newSrc, false).AsTask();
            imageSrc = newSrc;

            // not work in this place correctly, this line was moves to the OnLoadImageEvent method
            //await ImageCropper!.RevokeObjectUrlAsync(oldSrc).AsTask();
        }
    }
    catch (Exception exception)
    {
        _snackbarOptions.Content = exception.Message;
        PopupService!.EnqueueSnackbarAsync(_snackbarOptions);
    }
}
 public async void OnLoadImageEvent()
 {
// possible to check it to empty or whitespaces
       await ImageCropper!.RevokeObjectUrlAsync(OldSrc).AsTask(); // new line

       await JSRuntime!.InvokeVoidAsync("console.log", "Image Is loaded");
 }       

In general, it was a great solution to clean the redundant resource(s) (blobs) in OnLoadImageEvent method

@MaxymGorn MaxymGorn changed the title change copper src image old image WPF Hybrid: change copper src image old image Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants