Sirv image transformation .NET SDK

Official .NET SDK for the Sirv dynamic imaging API. This SDK provides a simple way to request any modified image (dimensions, format, quality, sharpen, crop, watermark etc.) using the 100+ image transformation options in Sirv's image optimization service.

.NET 6.0+ MIT License v2.0.0

Installation

dotnet add package Sirv.Client
using Sirv;

Quick Start

using Sirv;

var sirv = new SirvClient("demo.sirv.com", new Dictionary<string, object> { { "q", 80 } });

// Build a URL
sirv.Url("/image.jpg", new Dictionary<string, object> { { "w", 300 }, { "h", 200 }, { "format", "webp" } });
// => https://demo.sirv.com/image.jpg?q=80&w=300&h=200&format=webp

// Generate an image tag
sirv.Image("/photo.jpg", new ImageOptions { Alt = "A photo" });
// => <img class="Sirv" data-src="https://demo.sirv.com/photo.jpg" alt="A photo">

// Generate a script tag
sirv.ScriptTag(new ScriptTagOptions { Modules = new[] { "spin", "zoom" } });
// => <script src="https://scripts.sirv.com/sirvjs/v3/sirv.spin.zoom.js" async></script>

Constructor

new SirvClient(domain, defaults?)

ParameterTypeDescription
domainstringRequired. Sirv domain (e.g. "demo.sirv.com")
defaultsDictionary<string, object>?Default query parameters merged into every URL
var sirv = new SirvClient("demo.sirv.com");

var sirv = new SirvClient("demo.sirv.com", new Dictionary<string, object> { { "q", 80 } });

Url(path, parameters?)

Build a full Sirv URL. Nested dictionaries are flattened to dot-notation query parameters. Default parameters are merged with the provided params.

ParamTypeDescription
pathstringAsset path (e.g. "/image.jpg")
parametersDictionary<string, object>?Transformation parameters (nested dictionaries are flattened)
// Simple params
sirv.Url("/image.jpg", new Dictionary<string, object> { { "w", 300 }, { "h", 200 }, { "format", "webp" } });
// => https://demo.sirv.com/image.jpg?q=80&w=300&h=200&format=webp

// Nested params flatten to dot-notation
sirv.Url("/image.jpg", new Dictionary<string, object>
{
    { "crop", new Dictionary<string, object>
        {
            { "type", "face" },
            { "pad", new Dictionary<string, object> { { "width", 10 }, { "height", 10 } } }
        }
    }
});
// => ...?q=80&crop.type=face&crop.pad.width=10&crop.pad.height=10

// Params override defaults
sirv.Url("/image.jpg", new Dictionary<string, object> { { "q", 90 } });
// => https://demo.sirv.com/image.jpg?q=90

SrcSet(path, parameters?, options?)

Generate a srcset string for responsive images. Supports three modes:

Explicit widths

sirv.SrcSet("/image.jpg", new Dictionary<string, object> { { "format", "webp" } },
    new SrcSetOptions { Widths = new[] { 320, 640, 960, 1280, 1920 } });
// => https://demo.sirv.com/image.jpg?q=80&format=webp&w=320 320w,
//    https://demo.sirv.com/image.jpg?q=80&format=webp&w=640 640w, ...

Auto-generated widths (tolerance)

Generates widths between MinWidth and MaxWidth, stepping by current *= 1 + tolerance * 2.

sirv.SrcSet("/image.jpg", new Dictionary<string, object> { { "format", "webp" } },
    new SrcSetOptions { MinWidth = 200, MaxWidth = 2000, Tolerance = 0.15 });

Device pixel ratios (DPR)

Generates DPR variants with automatic variable quality: q = round(baseQ × 0.75(dpr-1)). Width and height are multiplied by the DPR.

sirv.SrcSet("/hero.jpg", new Dictionary<string, object> { { "w", 600 }, { "h", 400 } },
    new SrcSetOptions { DevicePixelRatios = new[] { 1.0, 2.0, 3.0 } });
// 1x: q=80, w=600, h=400
// 2x: q=60, w=1200, h=800
// 3x: q=45, w=1800, h=1200

SrcSetOptions

PropertyTypeDescription
Widthsint[]?Explicit list of widths
MinWidthint?Minimum width for auto-generation
MaxWidthint?Maximum width for auto-generation
Tolerancedouble?Step tolerance (default 0.15)
DevicePixelRatiosdouble[]?DPR values (e.g. new[] { 1.0, 2.0, 3.0 })

Image(path, options?)

Generate an <img> tag for a Sirv image with class="Sirv" and data-src.

ImageOptions

PropertyTypeDescription
TransformDictionary<string, object>?Transformation parameters for the URL
ViewerDictionary<string, object>?Viewer options (serialized to data-options)
Altstring?Alt text
ClassNamestring?Additional CSS class(es)
sirv.Image("/tomatoes.jpg", new ImageOptions { Alt = "Fresh tomatoes" });
// => <img class="Sirv" data-src="https://demo.sirv.com/tomatoes.jpg" alt="Fresh tomatoes">

sirv.Image("/photo.jpg", new ImageOptions
{
    Transform = new Dictionary<string, object> { { "w", 800 }, { "format", "webp" } },
    Viewer = new Dictionary<string, object> { { "autostart", "visible" }, { "threshold", 200 } },
    ClassName = "hero-image"
});
// => <img class="Sirv hero-image" data-src="...?q=80&w=800&format=webp"
//        data-options="autostart:visible;threshold:200">

Zoom(path, options?)

Generate a <div> tag for a Sirv zoom viewer with data-type="zoom".

ViewerDivOptions

PropertyTypeDescription
TransformDictionary<string, object>?Transformation parameters
ViewerDictionary<string, object>?Viewer options
ClassNamestring?Additional CSS class(es)
sirv.Zoom("/product.jpg");
// => <div class="Sirv" data-src="https://demo.sirv.com/product.jpg" data-type="zoom"></div>

sirv.Zoom("/product.jpg", new ViewerDivOptions
{
    Viewer = new Dictionary<string, object> { { "mode", "deep" }, { "wheel", false } }
});
// => <div ... data-type="zoom" data-options="mode:deep;wheel:false"></div>

Spin(path, options?)

Generate a <div> tag for a Sirv 360 spin viewer. No data-type attribute is added (Sirv JS auto-detects .spin files).

ViewerDivOptions

PropertyTypeDescription
ViewerDictionary<string, object>?Viewer options
ClassNamestring?Additional CSS class(es)
sirv.Spin("/product.spin");
// => <div class="Sirv" data-src="https://demo.sirv.com/product.spin"></div>

sirv.Spin("/product.spin", new ViewerDivOptions
{
    Viewer = new Dictionary<string, object> { { "autostart", "visible" }, { "autospin", "lazy" } }
});

Video(path, options?)

Generate a <div> tag for a Sirv video.

sirv.Video("/clip.mp4");
// => <div class="Sirv" data-src="https://demo.sirv.com/clip.mp4"></div>

Model(path, options?)

Generate a <div> tag for a Sirv 3D model viewer.

sirv.Model("/shoe.glb");
// => <div class="Sirv" data-src="https://demo.sirv.com/shoe.glb"></div>

ScriptTag(options?)

Generate a <script> tag to load Sirv JS. Optionally specify modules to load only what you need.

ScriptTagOptions

PropertyTypeDescription
Modulesstring[]?Modules to load (e.g. new[] { "spin", "zoom" })
AsyncboolAdd async attribute (default true)
sirv.ScriptTag();
// => <script src="https://scripts.sirv.com/sirvjs/v3/sirv.js" async></script>

sirv.ScriptTag(new ScriptTagOptions { Modules = new[] { "spin", "zoom" } });
// => <script src="https://scripts.sirv.com/sirvjs/v3/sirv.spin.zoom.js" async></script>

Full Examples

Responsive Product Page

using Sirv;

var sirv = new SirvClient("mystore.sirv.com", new Dictionary<string, object> { { "q", 80 } });

// Responsive image with srcset
var srcset = sirv.SrcSet("/hero.jpg",
    new Dictionary<string, object> { { "format", "webp" } },
    new SrcSetOptions { Widths = new[] { 320, 640, 960, 1280, 1920 } });
Console.WriteLine($"<img srcset=\"{srcset}\" sizes=\"100vw\">");

// Product gallery with zoom, spin, and video
var gallery = sirv.Gallery(new[]
{
    new GalleryItem { Src = "/products/shoe.spin" },
    new GalleryItem { Src = "/products/shoe-front.jpg", Type = "zoom" },
    new GalleryItem { Src = "/products/shoe-side.jpg", Type = "zoom" },
    new GalleryItem { Src = "/products/shoe-video.mp4" }
}, new GalleryOptions
{
    Viewer = new Dictionary<string, object> { { "thumbnails", "bottom" } }
});

// Load Sirv JS for spin + zoom
var script = sirv.ScriptTag(new ScriptTagOptions { Modules = new[] { "spin", "zoom" } });

Thumbnail Grid with Face Crop

var images = new[] { "/team/alice.jpg", "/team/bob.jpg", "/team/carol.jpg" };
var html = string.Join("\n", images.Select(path =>
    sirv.Image(path, new ImageOptions
    {
        Transform = new Dictionary<string, object>
        {
            { "w", 200 }, { "h", 200 },
            { "crop", new Dictionary<string, object> { { "type", "face" } } }
        },
        Alt = Path.GetFileNameWithoutExtension(path)
    })
));