Convert WebP to JPG, PNG, TIFF, and PDF in C#

In our previous post, we discussed WebP images and learned to convert WebP Images in Java. Today, in this article, we will learn to programmatically convert the WebP images into JPG, PNG, TIFF, and other formats using C#.

Convert WebP image to JPG, PNG or PDF formats in CSharp

First, we will have a look to convert the WebP images in the simplest way. Later we will convert with some customized options like tilt, flip, grayscale, resize, change gamma, contrast, and brightness, and add watermark to converted JPG images. Following are the quick links to topics:

Steps in this article and code samples are using GroupDocs.Conversion for NET. So please make sure to install the API from any of the following methods:

  • Install using NuGet Package Manager.
  • Download the DLL and reference it into the project.

Convert WebP to JPG in C#

To convert the WebP images into other formats, use the Converter class. For the simple conversion, you can use the below-mentioned few lines of C# code. This example shows the quick conversion of a WebP image to a JPG file. Just follow the steps:

  1. Instantiate the Converter object with the source WebP image.
  2. Instantiate the Image Conversion Options using ImageConvertOptions class and just set the Format to JPG.
  3. Call the Convert method with the output file path and the conversion options.
// Convert WebP image to JPG, PNG, BMP or any other format in C#
using (Converter converter = new Converter("./Resources/groupdocs_conversion-brand.webp"))
{
    ImageConvertOptions options = new ImageConvertOptions
    { // Set the conversion format to JPG
        Format = ImageFileType.Jpg
    };
    converter.Convert(@"./Output/converted-image.jpg", options);
}

Here are the original WebP image and the converted JPG image that is converted using the above code:

WebP Image
WebP Image
Converted from WebP to JPG
Converted JPG Image

Convert WebP to PNG, TIFF and other Image Formats in C#

Using the same above code and by just changing the file format i.e. “ImageFileType.Jpg” and the output file name, you may easily convert your WebP files into JPEG, PNG, TIF, TIFF, BMP, etc.

This was the simple conversion, now let us convert with different effects.

Convert WebP to JPG, PNG, TIFF with Advanced Options in C#

Along with the conversion of WebP to other formats, we can also add effects while converting. Below are some of the effects like; convert to grayscale; flip images horizontally or vertically; rotate the image to any angle; resize the image to make it smaller or larger; change the contrast, brightness, gamma values; or even apply watermarks to the converted images.

Converted from WebP to JPG
WebP to JPG
Converted from WebP to JPG in Grayscale
Grayscale
Converted from WebP to JPG with Resize
Resize
Converted from WebP to JPG with Horizontal Flip
Flip
Converted from WebP to JPG with changed Contrast
Contrast
Converted from WebP to JPG with Watermark
Watermark
Converted from WebP to JPG with Rotation
Rotate
Converted from WebP to JPG with Changed Brightness
Brightness
Converted from WebP to JPG with Gamma Change
Gamma

Here is the code that is used to apply these effects. You may apply these effects one by one or in combination to get the desired results.

// Apply effects while converting WebP image to other formats in C#
using (Converter converter = new Converter("./Resources/groupdocs_conversion-brand.webp"))
{
    ImageConvertOptions options = new ImageConvertOptions
    {
        Format = ImageFileType.Jpg,
        Grayscale = true,   // Convert the image in Grayscale
        Height = 141,       // Resize the Image Height
        Width = 167,        // Resize the image Width
        FlipMode = ImageFlipModes.FlipX,    // Flip the image
        Contrast = 50,      // Change the contrast of image
        RotateAngle = 90,   // Rotate the image
        Brightness = 50,    // Change the brightness
        Gamma = 0.5F,       // Gamma Setting
        Watermark =         // Watermark Settings
        {
            Text = "GroupDocs",
            Width = 100,
            Height = 100,
            Background = false,
            Top = 70,
            Left = 90,
            RotationAngle = -45,
        }
    };
    converter.Convert(@"./Output/converted-with-options.jpg", options);
}

Convert WebP to PDF in C#

Along with the conversion of WebP images to other image file formats, we can also convert images to PDF format. Following 3 lines of code will do the trick and help you converting the WebP image to PDF format.

// Convert WebP to PDF in C#
using (Converter converter = new Converter("./Resources/groupdocs_conversion-brand.webp"))
{
    PdfConvertOptions options = new PdfConvertOptions();
    converter.Convert(@"./Output/converted-webp-image.pdf", options);
}

For more details and advance options to convert into PDF, you may visit the documentation.

See Also

There are many other open-source examples that are publicly available at GitHub Repository. Download the source code and quickly run the examples using the getting started guide. In case of any difficulty, look at the documentation or reach us at any time on the forum.