A lightweight, fast image processing middleware for ASP.NET Core that enables on-the-fly image resizing, cropping, format conversion, and watermarking through simple URL query parameters.
Note: This is a maintained fork of ImageResizer.AspNetCore updated to support newer .NET versions (.NET 5.0+, including .NET 9.0 and .NET 10.0).
- Resize images - Scale images to specific dimensions
- Crop images - Extract specific regions from images
- Format conversion - Convert between PNG, JPG, and JPEG
- Image quality control - Adjust compression quality
- Auto-rotation - Automatically rotate images based on EXIF orientation
- Padding mode - Add padding to maintain aspect ratio
- Max mode - Scale to fit within bounds while maintaining aspect ratio
- Crop mode - Crop to exact dimensions
- Stretch mode - Stretch image to exact dimensions
- Watermarking - Add text or image watermarks
- Memory caching - Automatically cache processed images
- Multi-framework support - Supports .NET 5.0+
Install via NuGet:
dotnet add package Core5.ImageResizerOr using Package Manager Console:
Install-Package Core5.ImageResizerIn your Program.cs (ASP.NET Core 6+):
using ImageResizer.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add ImageResizer service
builder.Services.AddImageResizer();
var app = builder.Build();
// Use ImageResizer middleware
app.UseStaticFiles();
app.UseImageResizer();
app.Run();Or in Startup.cs (ASP.NET Core 5):
public void ConfigureServices(IServiceCollection services)
{
services.AddImageResizer();
}
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseImageResizer();
}Simply add query parameters to your image URLs:
<img src="~/images/photo.jpg?w=200" alt="Resized" />
<img src="~/images/photo.jpg?w=200&h=300" alt="Resized with height" />Resize to a specific width (height auto-calculated to maintain aspect ratio):
<img src="~/images/photo.jpg?w=300" alt="Width 300px" />Resize to a specific height (width auto-calculated):
<img src="~/images/photo.jpg?h=400" alt="Height 400px" />Resize to specific width and height with auto-detection of best fit:
<img src="~/images/photo.jpg?w=200&h=200" alt="Max fit" />Convert to PNG:
<img src="~/images/photo.jpg?format=png" alt="PNG format" />Convert to JPEG with quality setting:
<img src="~/images/photo.png?format=jpg&quality=80" alt="JPEG format" />Max mode (default) - Fit within bounds maintaining aspect ratio:
<img src="~/images/photo.jpg?w=200&h=200&mode=max" alt="Max fit" />Pad mode - Fit within bounds and pad with white space:
<img src="~/images/photo.jpg?w=200&h=200&mode=pad" alt="Padded" />Crop mode - Crop to exact dimensions from center:
<img src="~/images/photo.jpg?w=200&h=200&mode=crop" alt="Cropped" />Stretch mode - Stretch to exact dimensions (may distort):
<img src="~/images/photo.jpg?w=200&h=200&mode=stretch" alt="Stretched" />Adjust compression quality (1-100, default 100):
<img src="~/images/photo.jpg?w=800&quality=75" alt="Optimized" />Enable automatic EXIF-based rotation:
<img src="~/images/photo.jpg?autorotate=true" alt="Auto-rotated" />Use multiple parameters together:
<!-- Resize to 500x300, crop to fill, JPEG format, 80% quality -->
<img src="~/images/photo.jpg?w=500&h=300&mode=crop&format=jpg&quality=80" alt="Optimized" />
<!-- Resize width to 400, auto-height, PNG format, auto-rotate -->
<img src="~/images/photo.jpg?w=400&format=png&autorotate=true" alt="Responsive" />Add text watermark (requires ImageResizerJson.json configuration):
<img src="~/images/photo.jpg?w=400&wmtext=1" alt="With watermark" />Add image watermark:
<img src="~/images/photo.jpg?w=400&wmimage=1" alt="With image watermark" />| Parameter | Type | Default | Description |
|---|---|---|---|
w |
int | 0 | Width in pixels (0 = auto-calculate) |
h |
int | 0 | Height in pixels (0 = auto-calculate) |
mode |
string | max | Resize mode: max, pad, crop, stretch |
format |
string | original | Output format: jpg, jpeg, png |
quality |
int | 100 | JPEG quality (1-100) |
autorotate |
bool | false | Auto-rotate based on EXIF orientation |
wmtext |
int | 0 | Text watermark ID (requires config) |
wmimage |
int | 0 | Image watermark ID (requires config) |
To use watermarks, create an ImageResizerJson.json file in your wwwroot directory:
{
"WatermarkTextList": [
{
"Key": 1,
"Text": "© 2024 MyCompany",
"Color": "#FFFFFF",
"Size": 30,
"Opacity": 0.7
}
],
"WatermarkImageList": [
{
"Key": 1,
"ImagePath": "/images/watermark.png",
"Opacity": 0.5
}
]
}- Memory caching - Processed images are automatically cached in memory
- Fast processing - Uses SkiaSharp for efficient image manipulation
- Responsive - Query parameters are parsed only when needed
- Lightweight - Minimal dependencies
- Input: PNG, JPG, JPEG
- Output: PNG, JPG, JPEG
Works with all modern browsers that support standard image tags. The resizing happens server-side, so all clients can view the resized images.
See LICENSE file for details.
For issues, feature requests, or questions: