How to Convert WebP to JPEG in C#
By FoxLearn 1/10/2025 4:33:57 AM 104
However, if you're using C# and want to convert WebP images to another format, it can be challenging since direct support for WebP is limited in many libraries.
Google has released two DLLs, libwebp_x64.dll
and libwebp_x86.dll
, which can decode WebP images. However, these DLLs run native unmanaged code, so you’ll need to wrap them in a way that allows you to use them in managed code on a Windows machine.
Jose Pinero has done just that by creating a wrapper for these DLLs, allowing you to call their functionalities from C#. In this article, we'll walk you through the steps to convert a WebP image to a JPEG using C#.
Download libwebp_x64.dll, libwebp_x86.dll, and WebPWrapper.cs
To get started, you’ll need to download the following:
libwebp_x64.dll
andlibwebp_x86.dll
(Google's WebP library files)WebPWrapper.cs
(a C# wrapper for the WebP DLLs)
You can find these files in the GitHub repository created by Jose Pinero: WebP Wrapper GitHub Repository
In order to handle image processing in C#, you’ll need to use the System.Drawing
library, which is the standard library for image manipulation in Windows.
Install the System.Drawing.Common
NuGet package to make this available in your project:
You can extend the Image
class in C# to add a method for saving images as JPEGs with adjustable quality settings.
using System.Drawing; using System.Drawing.Imaging; using System.IO; using Encoder = System.Drawing.Imaging.Encoder; namespace WebPConverter { public static class ImageExtensions { // Static fields to store the JPEG encoder parameters and codec private static readonly EncoderParameters JpegEncoderParams = new EncoderParameters(1); private static readonly ImageCodecInfo JpegEncoder = GetEncoder(ImageFormat.Jpeg); static ImageExtensions() { // Pre-define the quality encoder parameter for JPEG JpegEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 80L); // Default quality (80) } public static void SaveJpeg(this Image img, string filePath, long quality = 80L) { // Update quality if needed JpegEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); img.Save(filePath, JpegEncoder, JpegEncoderParams); } public static void SaveJpeg(this Image img, Stream stream, long quality = 80L) { // Update quality if needed JpegEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); img.Save(stream, JpegEncoder, JpegEncoderParams); } private static ImageCodecInfo GetEncoder(ImageFormat format) { // We return the codec for the provided format return ImageCodecInfo.GetImageDecoders() .Single(codec => codec.FormatID == format.Guid); } } }
This code will allow you to save images as JPEGs with a specified quality, either to a file or a stream.
Convert the WebP Image to JPEG
With the WebPWrapper
and the ImageExtensions
class in place, you can now convert a WebP image to JPEG using the following simple code:
using System.Drawing; using WebPConverter; using WebPWrapper; WebP webp = new WebP(); Bitmap bitmap = webp.Load("d:\\myimg.webp"); bitmap.SaveJpeg("d:\\myimg.jpeg", 80);
This code loads a WebP image, converts it into a Bitmap
, and then saves it as a JPEG with a quality setting of 80.
- How to fix 'Failure sending mail' in C#
- How to Parse a Comma-Separated String from App.config in C#
- How to convert a dictionary to a list in C#
- How to retrieve the Executable Path in C#
- How to validate an IP address in C#
- How to retrieve the Downloads Directory Path in C#
- C# Tutorial
- Dictionary with multiple values per key in C#