Chronostasys / Blazor.Cropper

A blazor library provides component to crop image
MIT License
84 stars 19 forks source link

NullReferenceException when calling Cropper.GetCropedResult() #69

Closed ToonLinck closed 1 year ago

ToonLinck commented 1 year ago

Hi,

im gonna keep it short about what type of application I am building. The important part is that I am using a blazorise InputFile component to collect an IBrowserFile object to send to a Cropper component.

<InputFile id="cameraInput" type="file" accept="image/*" capture="camera" style="display: none;" 
   OnChange="HandleFileSelect"/>
           @if (_imageFile is not null)
             {
               <Cropper @ref="_cropperRef" ImageFile="_imageFile" InputId="cameraInput" CropperHeight="@(_windowHeight * 0.8)"> 
               </Cropper>
              }

The relevant variables like the IBrowserFile, _windowHeight or the reference objects are declared like this

    Cropper _cropperRef { get; set; }

    private IBrowserFile _imageFile;

    private double _windowHeight = 0;

    //more code

Now my problem is that as soon as the "save" function hits (where the cropped image gets processed) I get hit with a NullReferenceException when calling GetCropedResult().

public async Task Save()
    {
//some values get changed to enable different animations from the send-button etc. 
            StateHasChanged();

            if (_imageFile is not null)
            {
                var cropResult = await _cropperRef.GetCropedResult(); //this line throws the NullReferenceException

                byte[] bs = await cropResult.GetDataAsync();

                await SendImage(_imageFile, bs);
            }
    }

Screenshot 2023-06-13 112658

Through debugging I know that _cropperRef is not null I dont really know what could be causing the Exception to be thrown. Could I be missing a value I need to assign before I can call GetCropedResult or are there any steps I need to take that havent already been taken?

Thanks in advance.

Chronostasys commented 1 year ago

Hi,

im gonna keep it short about what type of application I am building. The important part is that I am using a blazorise InputFile component to collect an IBrowserFile object to send to a Cropper component.

<InputFile id="cameraInput" type="file" accept="image/*" capture="camera" style="display: none;" 
   OnChange="HandleFileSelect"/>
           @if (_imageFile is not null)
             {
               <Cropper @ref="_cropperRef" ImageFile="_imageFile" InputId="cameraInput" CropperHeight="@(_windowHeight * 0.8)"> 
               </Cropper>
              }

The relevant variables like the IBrowserFile, _windowHeight or the reference objects are declared like this

    Cropper _cropperRef { get; set; }

    private IBrowserFile _imageFile;

    private double _windowHeight = 0;

    //more code

Now my problem is that as soon as the "save" function hits (where the cropped image gets processed) I get hit with a NullReferenceException when calling GetCropedResult().

public async Task Save()
    {
//some values get changed to enable different animations from the send-button etc. 
            StateHasChanged();

            if (_imageFile is not null)
            {
                var cropResult = await _cropperRef.GetCropedResult(); //this line throws the NullReferenceException

                byte[] bs = await cropResult.GetDataAsync();

                await SendImage(_imageFile, bs);
            }
    }

Screenshot 2023-06-13 112658

Through debugging I know that _cropperRef is not null I dont really know what could be causing the Exception to be thrown. Could I be missing a value I need to assign before I can call GetCropedResult or are there any steps I need to take that havent already been taken?

Thanks in advance.

Hi @ToonLinck , thanks for reporting this. Which version of dotnet are you using? The stack trace you provides does not seem tobe complete, could you upload the full version? It seems that you are using imagefile and inputid at the same time, you could try to remove one to see if the issue persists.

ToonLinck commented 1 year ago

Hi @ToonLinck , thanks for reporting this. Which version of dotnet are you using? The stack trace you provides does not seem tobe complete, could you upload the full version? It seems that you are using imagefile and inputid at the same time, you could try to remove one to see if the issue persists.

@Chronostasys Removing the InputId Parameter fixed it!! Thanks a lot. I am working on version 1.2.14

Chronostasys commented 1 year ago

Hi @ToonLinck , thanks for reporting this. Which version of dotnet are you using? The stack trace you provides does not seem tobe complete, could you upload the full version? It seems that you are using imagefile and inputid at the same time, you could try to remove one to see if the issue persists.

@Chronostasys Removing the InputId Parameter fixed it!! Thanks a lot. I am working on version 1.2.14

Glad to hear that.