Images
Installation
Image processing is in the optional DotBlaze.Image package, backed by Magick.NET:
dotnet add package DotBlaze.ImageThe facade lives in the DotBlaze namespace. It accepts JPEG, PNG, and WebP images, up to 32 MiB of encoded data and 40 million pixels. Other formats are rejected before decoding. An image document represents a single decoded frame.
Processing Images
using var image = Image.Read(bytes);
image.Resize(1200, 800) // fit within these bounds, preserving aspect ratio
.Convert("webp")
.Optimize(80); // strip metadata and set encoder quality
byte[] result = image.Encode();Read accepts a byte array, a stream, or an IFormFile. It applies EXIF orientation when decoding. Dispose the returned document after use; caller-owned streams remain open.
Crop(x, y, width, height) crops a rectangle inside the current image. Width and Height report the current dimensions. Invalid dimensions, out-of-bounds crops, unsupported formats, and malformed input throw.
Convert accepts jpeg (or jpg), png, and webp. Optimize(quality) accepts 1–100 and removes metadata; JPEG and WebP use lossy quality. Encoded size depends on the source and selected format.
Storage and Uploads
using var image = Image.Read(Request.File("cover")!);
image.Resize(1200, 800).Convert("webp").Optimize();
await image.Save("covers/post.webp", disk: "public");
using var stored = await Image.FromStorage("covers/post.webp", disk: "public");Save uses Storage.Disk(disk).Put, so local, S3, and fake disks work the same way. Choose an output filename matching the format selected with Convert; Save does not infer a format from the filename.
Complete Example: Image Upload
Install DotBlaze.Image in the authenticated Blog application, then create:
// app/Http/Controllers/ImageUploadController.cs
using DotBlaze;
using ImageMagick;
using Microsoft.AspNetCore.Mvc;
using Image = DotBlaze.Image;
namespace Blog.Controllers;
public class ImageUploadController : BaseController
{
public IActionResult Create() => View();
[RequestSizeLimit(6 * 1024 * 1024)]
public async Task<IActionResult> Store()
{
var upload = Request.File("cover");
if (upload is null || upload.Length == 0 || upload.Length > 5 * 1024 * 1024)
return BadRequest(new { message = "Choose an image between 1 byte and 5 MiB." });
byte[] encoded;
try
{
using var image = Image.Read(upload);
encoded = image.Resize(1200, 800).Convert("webp").Optimize(80).Encode();
}
catch (Exception error) when (error is ArgumentException or MagickException)
{
return BadRequest(new { message = "The file is not a supported, valid image." });
}
var path = $"covers/{Guid.NewGuid():N}.webp";
await Storage.Disk("public").Put(path, encoded);
return Json(new { path, url = Storage.Disk("public").Url(path) });
}
}Inside Web.Map():
Route.Get<Blog.Controllers.ImageUploadController>("/image-upload", c => c.Create)
.Name("image-upload.create").Middleware("auth");
Route.Post<Blog.Controllers.ImageUploadController>("/image-upload", c => c.Store)
.Name("image-upload.store").Middleware("auth", "throttle:10,1,image-upload");<!-- resources/views/imageupload/Create.blade.html -->
@extends('layouts.app')
@section('content')
<form method="post" enctype="multipart/form-data" action="{{ Route('image-upload.store') }}">
@csrf
<label for="cover">Cover image (JPEG, PNG, or WebP; maximum 5 MiB)</label>
<input id="cover" name="cover" type="file" accept="image/jpeg,image/png,image/webp" required />
<button type="submit">Upload</button>
</form>
@endsectionRun dotblaze serve, sign in, and open /image-upload. Upload a valid PNG: the response contains a unique .webp path and public URL. A text file renamed .png is rejected based on its bytes, not its filename. File input accept is only a browser hint. Storage failures propagate instead of being misreported as invalid images. Use a private disk plus an authorized download endpoint for private media.
The real image decoding/storage regression is ImageViteTest.cs; run dotnet test tests/DotBlaze.Tests --filter FullyQualifiedName~ImageViteTest from the framework repository.