WebP is a new compressed image format developed by Google. If you want to convert WebP to any other format, you seem to be mostly out of luck if you are using C#.
There is, however, some tools out there. Google have released 2 DLL’s, libwebp_x64.dll and libwebp_x86.dll, but these DLL’s are running native unmanaged code, so to utilize these, we need to run our code on a Windows machine, and we need to wrap the code so it can be used from managed code.
Jose Pinero have done exactly this, wrapped the DLL’s so you can call the functionalities from your C# managed code.
So here is the journey on how to convert a WebP image to a JPEG using C#.
STEP 1: DOWNLOAD libwebp_x64.dll AND libwebp_x86.dll AND WebPWrapper.cs
Go to the following GIT repo and get the WebPWrapper.cs. Don’t forget the 2 Google DLL’s as well, libwebp_x64.dll and libwebp_x86.dll:
https://github.com/JosePineiro/WebP-wrapper
STEP 2: INCLUDE SYSTEM.DRAWING
System.Drawing is the old image library native to Windows. Include the package:
https://www.nuget.org/packages/System.Drawing.Common/
STEP 3: MAKE AN JPEG EXTENSION FOR THE IMAGE CLASS
The C# Image class can easily be extended to create a JPEG image:
using System.Drawing;
using System.Drawing.Imaging;
using Encoder = System.Drawing.Imaging.Encoder;
namespace WebPConverter
{
public static class ImageExtensions
{
public static void SaveJpeg(this Image img, string filePath, long quality)
{
var ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.Quality, quality);
img.Save(filePath, GetEncoder(ImageFormat.Jpeg), ep);
}
public static void SaveJpeg(this Image img, Stream stream, long quality)
{
var ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(Encoder.Quality, quality);
img.Save(stream, GetEncoder(ImageFormat.Jpeg), ep);
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
return codecs.Single(codec => codec.FormatID == format.Guid);
}
}
}
STEP 4: CONVERT THE WEBP IMAGE TO JPEG
This simple code will convert a WebP image to a Jpeg image:
using System.Drawing;
using WebPConverter;
using WebPWrapper;
WebP webp = new WebP();
Bitmap bitmap = webp.Load("d:\\1.webp");
bitmap.SaveJpeg("d:\\1.jpeg", 80);
RUNNING THE WebPWrapper.cs AS A SERVICE
Jose Pinero have tied in some GUI related code in the WebPWrapper.cs code, so if you need to run the code as a service, an API, or as a worker process you need to clean up some of his code.
The easiest way to do this is to remove the following line from the WebPWrapper.cs file and handle whatever compiler error is appearing, by deleting the lines that cast an error.
using System.Windows.Forms;
CONVERTING AND UPLOADING TO BLOB STORAGE:
This example is larger, as I am grabbing a WebP image from a website, converting it into a JPEG, and storing that image in a Blob storage:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Drawing;
using WebPConverter;
using WebPWrapper;
// Grab a WebP image from a Google Website
HttpClient client = new HttpClient();
var response = await client.GetAsync("https://www.gstatic.com/webp/gallery/1.webp");
// Open a connection to a Blob Container
CloudBlobContainer cloudBlobContainer = CloudStorageAccount.Parse(YOUR_CONNECTiONSTRING).CreateCloudBlobClient().GetContainerReference("containerName");
// Give the blob a name and create a blob reference
string blobName = "MediaStorage/test/image1.jpg";
var blobReference = cloudBlobContainer.GetBlockBlobReference(blobName);
blobReference.Properties.ContentType = "image/jpg";
// Convert the WebP image to a JPEG and stream it into the Blob file we created
await using var jpgStream = await ConvertWebPContentToJpegContent(response.Content);
jpgStream.Seek(0, SeekOrigin.Begin);
await blobReference.UploadFromStreamAsync(jpgStream);
async Task<Stream> ConvertWebPContentToJpegContent(HttpContent content)
{
// Grab the raw WebP image
var rawWebP = await content.ReadAsByteArrayAsync();
// Convert the WebP image to a JPEG
Bitmap bitmap = new WebP().Decode(rawWebP);
MemoryStream memoryStream = new MemoryStream();
// Save the JPEG to a Stream that can be saved in a blob storage
bitmap.SaveJpeg(memoryStream, 80);
return memoryStream;
}
FINAL NOTES:
I’m not sure why it’s so hard to convert WebP to JPEG using C#, if it’s because WebP is not widely used, or WebP is so superior that no one would ever convert to another format. The fact that the code is tied to a Windows machine can be a deal breaker for some. There are paid products out there that maybe will help you.
That’s it. You are now a WebP expert. Happy coding.
MORE TO READ: