CropAndWebP.TagHelper
1.0.0
See the version list below for details.
dotnet add package CropAndWebP.TagHelper --version 1.0.0
NuGet\Install-Package CropAndWebP.TagHelper -Version 1.0.0
<PackageReference Include="CropAndWebP.TagHelper" Version="1.0.0" />
<PackageVersion Include="CropAndWebP.TagHelper" Version="1.0.0" />
<PackageReference Include="CropAndWebP.TagHelper" />
paket add CropAndWebP.TagHelper --version 1.0.0
#r "nuget: CropAndWebP.TagHelper, 1.0.0"
#:package CropAndWebP.TagHelper@1.0.0
#addin nuget:?package=CropAndWebP.TagHelper&version=1.0.0
#tool nuget:?package=CropAndWebP.TagHelper&version=1.0.0
CropAndWebP.TagHelper - Crop and WebP Conversion
Consistent image dimensions, ideal for thumbnail lists, cards and galleries
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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Razor (>= 2.3.13)
- SixLabors.ImageSharp (>= 3.1.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release.
Automatic image cropping and WebP conversion.
Supports lazy loading, fetch priority, CLS-friendly dimensions and SEO-friendly markup.