Skip to main content

Embed image processing in your ASP.NET Core application with ImageSharp.Web

For an application we are building, we had to image processing features to an existing API. Our first idea was to build our own solution but then we discovered ImageSharp.Web. ImageSharp.Web builts on top of the great ImageSharp library and adds middleware to manipulate images. Exactly what we needed!

The documentation is quite limited so let me help you out and walk you through the steps(and share a few gotcha’s along the way) to get it up and running in your ASP.NET Core application.

Installation

First you need to install the library through NuGet:

dotnet add package SixLabors.ImageSharp.Web

Now you need to register the middleware:

And add it to the ASP.NET Core pipeline:

Invoke the middleware

To invoke the middleware, you should browse to the application and add the commands you want to execute. For example, to resize an image, you should call:

http://localhost/imageprocessingexample/exampleimage.png?width=400

Sounds easy? Unfortunately there are a few gotcha’s I encountered.

Static Files middleware

The code I’ve shared so far can also be found in the ImageSharp.Web documentation. But when you try to run the application and call the configured middleware it will fail. This is because some extra work needs to be done depending on the source of your images. Therefore the ImageSharp.Web library uses the concept of ‘Image providers’. An image provider is a specific source where the middleware will try to load the image from. The default middleware will look for the image as static content in the ‘wwwroot’ folder of your ASP.NET core application.

To make this image provider work it is necessary to have the static file middleware added to your request pipeline and of course a ‘wwwroot’ folder with the image you want to manipulate.

So we have to update our code example above to the following:

It is important to notice here that we should add the StaticFiles middleware AFTER the ImageSharp middleware.

ImageSharp 1 vs ImageSharp 2

When I now tried to run the application again, I encountered a second issue. The ImageSharp middleware was invoked correctly but throwed the following exception:

 An unhandled exception occurred while processing the request.

MissingMethodException: Method not found: 'System.Threading.Tasks.Task`1<SixLabors.ImageSharp.Formats.IImageFormat> SixLabors.ImageSharp.Image.DetectFormatAsync(SixLabors.ImageSharp.Configuration, System.IO.Stream)'

The root cause of this issue became obvious when I took a look at my dependencies:

I’m using a 1.x version of ImageSharp.Web and a 2.x version of ImageSharp. ImageSharp.Web is only compatible with the 1.x version of ImageSharp. I fixed it by explicitly specifying the correct version in my project:

Popular posts from this blog

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Color B

Help! I accidently enabled HSTS–on localhost

I ran into an issue after accidently enabling HSTS for a website on localhost. This was not an issue for the original website that was running in IIS and had a certificate configured. But when I tried to run an Angular app a little bit later on http://localhost:4200 the browser redirected me immediately to https://localhost . Whoops! That was not what I wanted in this case. To fix it, you need to go the network settings of your browser, there are available at: chrome://net-internals/#hsts edge://net-internals/#hsts brave://net-internals/#hsts Enter ‘localhost’ in the domain textbox under the Delete domain security policies section and hit Delete . That should do the trick…

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.