When building your apps you might have the need to compress your images in a way that you still wouldn’t lose the quality. Heavy image assets may have a toll on application performance, the best image formats are the network-friendly type.
I will demonstrate how to easily use image compression in your web applications using this Libwebp.Net an asynchronous utility for encoding images to Google’s .webp format. Seamlessly compress images to lossy and lossless formats in your .NET projects to improve network performance and reduce file size on disk. WebP generally has better compression than JPEG, PNG, and GIF and is designed to supersede them. You can see the library in action by using the webclient, your result will be downloaded into your browser.
Using the Library
General Usage
Below shows the basic use of the library using a console app.
Asp.Net Core
Below demonstrates how the library is used in Asp.Net Core to convert uploaded images. This library is currently supported only in Windows Environments.
[Coming SOON!!!] Asp.Net Core MiddleWare
Libewebp.Net.Middleware
is a separate package that depends on Libewebp.Net
and allows you to inject image compression in your Asp.Net pipeline. This middleware will compress your specified image types using lossy and lossless algorithms.
Advanced Encoding
The encoder contains a lot advanced parameters. LibWebP.Net supports libWebp’s advanced encoding API which can be used to better balance the trade-off between compression efficiency and processing time. You can get access to the advanced encode parameters by adding the various options below to your WebpConfigurationBuilder
Options
These options are currently supported in the library, use it to manipulate the compression algorithm for your desired output.
Option | Description |
AlphaQ(int value) | transparency-compression quality (0..100) |
CompressionMethod(int value) | compression method (0=fast, 6=slowest) |
Lossless() | encode image losslessly |
Preset(string value) | one of default, photo, picture, drawing, icon, textd |
QualityFactor(float value) | quality factor (0:small..100:big) |