Cropper.Blazor 1.2.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Cropper.Blazor --version 1.2.5
NuGet\Install-Package Cropper.Blazor -Version 1.2.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cropper.Blazor" Version="1.2.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cropper.Blazor --version 1.2.5
#r "nuget: Cropper.Blazor, 1.2.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cropper.Blazor as a Cake Addin
#addin nuget:?package=Cropper.Blazor&version=1.2.5

// Install Cropper.Blazor as a Cake Tool
#tool nuget:?package=Cropper.Blazor&version=1.2.5

Cropper.Blazor is a component that wraps around Cropper.js version 1.6.0

Build and run test Deploy to GitHub Pages Deploy to NuGet coverage GitHub GitHub NuGet Badge

Demo

API

Prerequisites

  • Supported .NET 7.0, .NET 6.0 versions for these web platforms:
    • Blazor WebAssembly
    • Blazor Server
    • Blazor Server Hybrid with MVC
    • MAUI Blazor Hybrid

Installation

dotnet add package Cropper.Blazor

Usage

Import Custom Element:

@using Cropper.Blazor.Components

Add the following to index.html (client-side — Blazor Webassembly, Blazor MAUI) or _Host.cshtml (server-side — Blazor Server, MVC with Blazor Server) in the head

<link href="_content/Cropper.Blazor/cropper.min.css" rel="stylesheet" />

Add the following to index.html or _Host.cshtml in the body

<script src="_content/Cropper.Blazor/cropper.min.js"></script>

Add the following to the relevant sections of Program.cs

using Cropper.Blazor.Extensions;
builder.Services.AddCropper();

In addition, you can change the path to the CropperJSInterop.min.js module if for some reason it is located outside the server root folder as follows:

  • Override internal path to CropperJSInterop.min.js module:
builder.Services.AddCropper(new CropperJsInteropOptions()
{
    DefaultInternalPathToCropperModule = "<YourPath>/_content/Cropper.Blazor/cropperJsInterop.min.js"
})
  • Override full global path to CropperJSInterop.min.js module:
builder.Services.AddCropper(new CropperJsInteropOptions()
{
    IsActiveGlobalPath = true,
    GlobalPathToCropperModule = "<StartUrlWithPath>/_content/Cropper.Blazor/cropperJsInterop.min.js"
})

Also for server-side (Blazor Server or MVC with Blazor Server) you need add configuration SignalR, increase MaximumReceiveMessageSize of a single incoming hub message (default is 32KB) and map SignalR to your path. For example:

builder.Services.AddServerSideBlazor()
    .AddHubOptions(options =>
    {
        options.MaximumReceiveMessageSize = 32 * 1024 * 100;
    });
app.MapBlazorHub();

And then use it in Razor file (for example):

<CropperComponent
  Class="cropper-demo"
  ErrorLoadImageClass="cropper-error-load"
  @ref="cropperComponent"
  OnCropStartEvent="OnCropStartEvent"
  OnCropEndEvent="OnCropEndEvent"
  OnCropEvent="OnCropEvent"
  OnZoomEvent="OnZoomEvent"
  OnCropMoveEvent="OnCropMoveEvent"
  OnReadyEvent="OnCropReadyEvent"
  OnLoadImageEvent="OnLoadImageEvent"
  Src="@Src"
  InputAttributes="@InputAttributes"
  ErrorLoadImageSrc="@ErrorLoadImageSrc"
  IsErrorLoadImage="@IsErrorLoadImage"
  OnErrorLoadImageEvent="OnErrorLoadImageEvent"
  Options="options">
</CropperComponent>

And then use it in *.razor.cs file

You may override Cropper JavaScript module with execution script which can replace 6 event handlers (onReady, onCropStart, onCropMove, onCropEnd, onCrop, onZoom), such as overriding the onZoom callback in JavaScript:

window.overrideCropperJsInteropModule = (minZoomRatio, maxZoomRatio) => {
    window.cropper.onZoom = function (imageObject, event, correlationId) {
        const jSEventData = this.getJSEventData(event, correlationId);
        const isApplyPreventZoomRatio = minZoomRatio != null || maxZoomRatio != null;

        if (isApplyPreventZoomRatio && (event.detail.ratio < minZoomRatio || event.detail.ratio > maxZoomRatio)) {
            event.preventDefault();
        }
        else {
            imageObject.invokeMethodAsync('CropperIsZoomed', jSEventData);
        }
    };
};

Analysis of the signature onReady, onCropStart, onCropMove, onCropEnd, onCrop, onZoom event handlers:

imageObject

  • Type: Object

Reference to base cropper component.

event

  • Type: CustomEvent

Represent Cropper Event.

correlationId

  • Type: String
  • Default: Cropper.Blazor

A Correlation ID is a unique identifier that is added to the very first interaction(incoming request) to identify the context and is passed to all components that are involved in the transaction flow.

Definitely need to tell these rules in Blazor:

await JSRuntime!.InvokeVoidAsync("window.overrideCropperJsInteropModule", MinZoomRatio, MaxZoomRatio);

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b feature/<my-new-feature>
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin feature/<my-new-feature>
  5. Submit a pull request 😄
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.2 831 3/30/2024
1.3.1 3,740 2/2/2024
1.3.0 4,595 11/18/2023
1.2.7 322 11/9/2023
1.2.6 1,309 10/8/2023
1.2.5 1,030 9/12/2023
1.2.4 826 7/24/2023
1.2.3 248 7/15/2023
1.2.2 492 6/23/2023
1.2.1 117 6/22/2023
1.2.0 3,619 5/1/2023
1.1.4 1,680 4/1/2023
1.1.3 754 2/27/2023
1.1.2 619 2/3/2023
1.1.1 562 11/24/2022
1.1.0 313 11/12/2022
1.0.2 429 11/3/2022
1.0.1 314 11/2/2022