CropAndWebP.TagHelper 1.0.1

dotnet add package CropAndWebP.TagHelper --version 1.0.1
                    
NuGet\Install-Package CropAndWebP.TagHelper -Version 1.0.1
                    
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="CropAndWebP.TagHelper" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CropAndWebP.TagHelper" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CropAndWebP.TagHelper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CropAndWebP.TagHelper --version 1.0.1
                    
#r "nuget: CropAndWebP.TagHelper, 1.0.1"
                    
#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.
#:package CropAndWebP.TagHelper@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CropAndWebP.TagHelper&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=CropAndWebP.TagHelper&version=1.0.1
                    
Install as a Cake Tool

CropAndWebP.TagHelper - Crop Images and Convert to WebP Automatically

Create SEO-friendly, high-performance thumbnails with consistent dimensions for galleries, product listings, cards, blogs, and responsive websites

Source Code 🔗 GitHub Repository →

Crop JPG, JPEG, PNG, and GIF images and automatically convert them to WebP

Supports lazy loading, fetch priority, CLS reduction, and SEO-friendly markup.


Features

  • ✅ Automatic center cropping to fixed dimensions
  • ✅ Consistent thumbnail sizes regardless of source image dimensions
  • ✅ Ideal for thumbnail galleries, cards, and listings
  • ✅ Automatic WebP conversion
  • ✅ Automatic width and height detection
  • ✅ CLS reduction
  • ✅ Lazy loading support
  • ✅ Fetch priority support
  • ✅ SEO-friendly markup
  • ✅ Lightweight and fast
  • ✅ Easy to use

Installation

Package Manager

Install-Package CropAndWebP.TagHelper

.NET CLI

dotnet add package CropAndWebP.TagHelper

Registration

Register the Tag Helper in your Razor Pages or MVC project:

@addTagHelper *, CropAndWebP.TagHelper

View

@page
@model IndexModel
@inject IWebHostEnvironment env
@addTagHelper *, CropAndWebP.TagHelper

@{
    var webRootImagePath = System.IO.Path.Combine(env.WebRootPath, "images", "photo1.jpg");
    var externalImagePath = System.IO.Path.Combine(@"D:\data", "images", "photo2.jpg"); 
}

Basic usage of the `CropAndWebP` Tag Helper:

 <CropAndWebP cropHeight="200" cropWidth="200" firstResizeThanCrop="false"
        SourceImagePath="@webRootImagePath" Url="/images/photo1.jpg" />

 <CropAndWebP cropHeight="100" cropWidth="100" firstResizeThanCrop="false"
        SourceImagePath="@externalImagePath" Url="/external-images/photo2.jpg" />

Generated HTML

On the initial page load, only an <img> element is rendered because the WebP images are generated in the background. No <source> elements are available yet.

<picture>
  <img  src="/images/photo1.jpg" alt="photo1" width="1000" height="750">
</picture>

After the generation process has completed, the corresponding <source> elements with srcset attributes become available.

<picture>
  <source srcset="/images/photo1-size_200x200.webp">
  <img  src="/images/photo1.jpg" alt="photo1" width="200" height="200">
</picture>

Css , optional

 picture:not(:has(source)) > img {
            border: 2px solid #00f;
            max-width: 100%;
            height: auto;
        }

        /* Picture mit source */
        picture:has(source) > img {
            border: 2px solid #f00;
        }
        

Controller for External Images Only


using Microsoft.AspNetCore.Mvc;
namespace WebRazor10.Controllers
{
    public class ImagesController : Controller
    {
        [HttpGet("/external-images/{fileName}")]
        public IActionResult GetImage(string fileName)
        {
            var imagePath = Path.Combine(@"D:\data", "images", fileName);
            if (!System.IO.File.Exists(imagePath))
            {
                return NotFound();
            }

            var extension = Path.GetExtension(fileName).ToLowerInvariant();
            var contentType = extension switch
            {
                ".jpg" or ".jpeg" => "image/jpeg",
                ".png" => "image/png",
                ".gif" => "image/gif",
                ".webp" => "image/webp",
                _ => "application/octet-stream"
            };
            return PhysicalFile(imagePath, contentType);
        }
    }
}

Available Attributes

Attribute Description
SourceImagePath (required) Physical path to the source image on the server.
Url (required) Public image URL used in the generated HTML output.
cropWidth Output image width after processing and cropping.
cropHeight Output image height after processing and cropping
firstResizeThanCrop If true, the image is resized before cropping. Default: true.
cssClass CSS classes added to the generated img element.
cssStyle Optional inline styles added to the generated img element.
Alt Alternative text for accessibility and SEO.
Attribute Additional attributes added to the generated img element.
isLazyLoading Adds loading="lazy" to the generated img element.
isFetchpriorityHigh Adds fetchpriority="high" to the generated img element.
width, height Generated automatically based on the processed image dimensions.

Supported Formats

Input

  • JPG
  • JPEG
  • PNG
  • GIF

Output

  • WebP

Requirements

  • .NET 8 or later
  • ASP.NET Core
  • SixLabors.ImageSharp
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.0.1 100 10/11/2026
1.0.0 95 10/10/2026

Automatic image cropping and WebP conversion.
 Supports lazy loading, fetch priority, CLS-friendly dimensions and SEO-friendly markup.