A field guide to responsive image delivery
Dynamic
image resizing.
Keep one high-resolution original on the server. Resize it for the browser's needs when requested, choose cropping separately, and cache the result for the next visitor.
One source image.
Several useful responses.
Dynamic image resizing (also called on-the-fly resizing) means the server transforms an original while handling an image request. Depending on the selected processor, it can resize, crop, rotate, and re-encode the file before returning the response.
Responsive image delivery is the browser-facing part. HTML provides a set of candidate URLs with srcset and describes the rendered slot with sizes. The browser chooses a candidate for its layout and pixel density. The server then creates or retrieves that URL's variant.
Keep a good original
Store the largest practical source once. Do not make a separate hand-exported file for every device width.
Describe the variant
Put bounded transformation commands in the URL: width, an optional fit box or crop mode, output format, and quality. Exact crop commands differ between libraries.
Return fewer bytes
The browser receives the smaller encoded image—not the original followed by a CSS shrink.
Important distinction: CSS such as width: 100%, Bootstrap .img-fluid, and object-fit: cover change how an image is drawn. By themselves they do not reduce the bytes downloaded.
Razor renders the URL.
Middleware renders the image.
This site uses .NET 10, server-rendered Razor components, and the Interactive Server render mode. The component produces ordinary HTML; the ImageResize middleware handles each image as a separate HTTP request, outside the Blazor SignalR circuit.
Original stays on disk
wwwroot/images/001/…jpg
Razor writes candidates
srcset and sizes describe the image slot.
Browser picks a URL
Layout width × device pixel ratio informs the choice.
Nativeloading="lazy" defers off-screen filesImageResize handles it
SkiaSharp middleware reads the source and fits it to the requested bounds.
Before the static-asset endpointSend and cache a variant
Return the encoded result with normal HTTP caching headers.
Later identical requests reuse cached output/images/001/01-01a-g.jpg?width=640&height=480&quality=88
The 2,752 × 1,536 JPEG is fit inside a 640 × 480 box, preserving its 16:9 aspect ratio, so the response is 640 × 357. ImageResize uses SkiaSharp's Mitchell cubic resampler; JPEG quality 88 controls encoding artifacts. The original stays unchanged and the first result is cached for later identical URLs. The card's 4:3 frame is cropped visually by CSS.
The first HTML is rendered on the server. Once the browser parses it, it requests the image URL directly over HTTP. The resized JPEG response does not travel through the Blazor SignalR circuit. ImageResize is registered before this app's static-asset endpoints, reads from the images content root, and stores cached derivatives under App_Data/image-cache. Requests without resize parameters continue as original static assets.
The gallery component reads filenames, dimensions, and original file sizes on the server to render its cards. It does not read every image into SignalR or send the image data in a component event.
The viewport changes the slot.
The browser chooses the bytes.
The server does not magically know the viewport from a Razor component. The page publishes candidate image URLs, while sizes tells the browser approximately how wide the image will render at each Bootstrap breakpoint. These estimates account for the desktop navigation column, the page shell's maximum width and padding, and Bootstrap gutters.
row-cols-xl-41,440 px viewport · ≈248 CSS px≈496 source pixelsrow-cols-lg-31,024 px viewport · ≈211 CSS px≈422 source pixelsrow-cols-sm-2768 px viewport · ≈209 CSS px≈418 source pixelsrow-cols-1390 px viewport · ≈350 CSS px≈700 source pixels<img
src="/images/001/01-01a-g.jpg?width=640&height=480&quality=88"
srcset="/images/001/01-01a-g.jpg?width=320&height=240&quality=88 320w,
/images/001/01-01a-g.jpg?width=640&height=480&quality=88 640w,
/images/001/01-01a-g.jpg?width=960&height=720&quality=88 960w,
/images/001/01-01a-g.jpg?width=1440&height=1080&quality=88 1440w"
sizes="(min-width: 1840px) 332px, (min-width: 1818px) calc(378px - min(46px, 2.5vw)),
(min-width: 1200px) calc(22.5vw - 76.5px), (min-width: 992px) calc(30vw - 96.67px),
(min-width: 641px) calc(45vw - 137px), (min-width: 576px) calc(45vw - 8px),
(min-width: 400px) 90vw, calc(100vw - 40px)"
width="640" height="357" loading="lazy" decoding="async"
alt="High-resolution source image 01-01a-g.jpg">
The width descriptor (for example 640w) must match the generated image width. The browser combines sizes, its device-pixel ratio, supported formats, and its own network heuristics to choose a candidate. Every candidate has the same fit and encoding commands, so it can safely switch widths when the viewport or zoom changes.
max-width: 100% prevents overflow.
It makes an image fit its container after the browser has a resource. It cannot retroactively make the original transfer smaller.
width changes the response pixels.
Each selected URL can request an appropriately sized derivative. Fixed candidates also prevent a client from inventing unlimited URL variants through this page.
object-fit: cover is visual.
The gallery requests a 16:9 fitted JPEG, then CSS crops it inside the Bootstrap 4:3 frame. The response still contains the side pixels hidden by cover. Use a server processor with a crop command when you want to avoid transferring pixels outside the visible composition.
Do not lazy-load the LCP image. This gallery is below the explanatory content, so its cards use loading="lazy". A prominent first-screen hero image should usually be eager (or explicitly prioritized) and should have its dimensions or aspect ratio reserved to avoid layout shift.
Choose one handler
for a given image URL.
ImageResize 3.5.0 is active and Core5.ImageResizer 10.0.1 is installed as an alternative. ImageSharp.Web 3.3.0 / ImageSharp 3.2.0 are documented below, but not referenced: despite this repository's MIT license qualifying for the Apache 2.0 grant, the v3 package's build target still requires a valid Six Labors key for Release builds. It is not license-key-free. Run only one image middleware for a given URL path.
SkiaSharp-backed middleware with disk cache
The package targets .NET 10, decodes JPEG, PNG, WebP, GIF, BMP, and TIFF, and preserves the original format. This app's configuration monitors the images root, prevents upscaling, defaults to JPEG/WebP quality 88, caps dimensions at 1,440 × 1,080 and quality at 90, and limits its disk cache to 1 GB.
Version 3.5.0's SkiaSharp codec uses the Mitchell cubic resampler. Its URL does not expose a sampler switch: quality=88 changes JPEG/WebP encoding quality, not edge interpolation. Raising quality can reduce compression artifacts, but it cannot remove aliasing produced during resizing. ImageSharp.Web can select Lanczos3 if a valid Six Labors license key is configured for Release builds.
License: MIT. It is a distinct implementation from ImageSharp.Web, not a second layer to run on the same request.
dotnet add package ImageResize --version 3.5.0
using ImageResize.Core.Extensions;
var imageCacheRoot = Path.Combine(
builder.Environment.ContentRootPath, "App_Data", "image-cache");
Directory.CreateDirectory(imageCacheRoot);
builder.Services.AddImageResize(options =>
{
options.ContentRoots = ["images"];
options.WebRoot = builder.Environment.WebRootPath;
options.CacheRoot = imageCacheRoot;
options.AllowUpscale = false;
options.DefaultQuality = 88;
options.Bounds.MaxWidth = 1440;
options.Bounds.MaxHeight = 1080;
options.Bounds.MaxQuality = 90;
options.Cache.MaxCacheBytes = 1_073_741_824;
options.Cache.PruneOnStartup = true;
});
app.UseImageResize();
app.MapStaticAssets();private static string ResizeUrl(string source, int width) =>
$"{source}?width={width}&height={width * 3 / 4}&quality=88";
private static string SourceSet(string source) =>
string.Join(", ", ResponsiveWidths.Select(width => $"{ResizeUrl(source, width)} {width}w"));
<img src="@ResizeUrl(image.Url, 640)"
srcset="@SourceSet(image.Url)" sizes="@ImageSizes"
width="640" height="357" loading="lazy"
decoding="async" alt="@image.Alt" />/images/001/photo.jpg?width=640&height=480&quality=88The tabs compare package setup and URL syntax; only the selected code sample changes. ImageResize is the middleware currently configured at startup. To run Core5 instead, change the server registration and URL builder, then verify its ordering against this app's static-asset endpoints. To run ImageSharp.Web, first provide a valid Six Labors license key/file for Release builds, add the packages, and replace the active middleware. Never run multiple image handlers on the same path.
Fast after the first transform.
Still a server workload.
Resizing on demand trades storage duplication for CPU work on a cache miss. Good URL design, cache policy, and bounded parameters make that trade worthwhile.
One URL, one representation
Width, crop, format, and quality all affect the result. A finite set of candidate URLs prevents needless cache variants; plan how to invalidate derivatives when a source changes.
Bound dimensions and work
Do not trust arbitrary query values. Restrict source paths and formats, enforce maximum width/height/quality, prevent unwanted upscaling, and cap cache storage. For public transformation URLs, consider ImageSharp.Web HMAC signing so clients cannot freely invent costly variants.
Decode, transform, encode
The first uncached request costs CPU and memory. Warm popular variants if needed; monitor cache misses and avoid decoding huge originals without an input-size limit.
Use browser and edge caching
ETags and cache-control help repeat visits. A CDN can cache the transformed URL near users, provided its cache key includes the transformation parameters.
Sampler and encoder quality are different
ImageResize uses SkiaSharp's Mitchell cubic sampler; this version does not expose a URL-level filter switch. JPEG quality=88 controls compression artifacts, not pixel interpolation, so it may improve blockiness but cannot change a jagged edge created during downsampling. ImageSharp.Web offers rsampler=lanczos3 and compand=true if a valid license key is available. Compare at the actual card size; stronger filters can create halos and cannot restore missing source detail.
Lazy loading is a different lever
loading="lazy" delays off-screen requests; resizing reduces the bytes in each request. For thousands of records, also page or virtualize the rendered cards.
Good default: store originals privately or in a controlled source folder, publish a small set of responsive widths, decide whether to crop on the server or in CSS, cache each representation, and verify real response sizes in browser DevTools.
328 original JPEGs.
Request only the pixels you need.
Every JPEG in folders 001 through 008 is included: 328 source files, each 2,752 × 1,536 pixels. Bootstrap cards use the same header / 4:3 image / byte footer pattern as Lazy Loading. The browser chooses one server-fitted JPEG candidate per image; CSS applies the card's cover crop.
ImageResize · activeScroll through the cards and resize the browser. Each footer compares the original JPEG size with the loaded response size and shows the percentage saved. Gallery totals update as images load; they cover all 328 files once every response has been measured. Response bytes come from the same-origin Resource Timing API and exclude HTTP headers.








































































































































































































































































































































row-cols-1 → 2 → 3 → 4Open Network in DevTools.
Filter by Img. The request URL shows the selected width and quality. ImageResize preserves the JPEG source's 16:9 ratio; CSS crops the display to the card's 4:3 frame. On a narrow viewport the browser should generally choose fewer pixels than a high-density desktop card.
Compare body bytes, not only dimensions.
The footer reports original and resized JPEG bytes, then calculates the percentage saved for that image. Encoder quality, image detail, selected width, and cache status all affect the result, so exact savings differ from one image to another.