How to save images in .webp and reduce image size without quality

How to save images in .webp and reduce image size without quality in Asp.net C#

To save images in the .webp format and reduce their size without losing quality, you can use libraries like `ImageSharp` or `ImageSharp.Web`. These libraries provide functionality to encode images to the WebP format and optionally apply compression to reduce file size.


Here's how you can modify your code to use `ImageSharp` to save images in .webp format and reduce their size:


```csharp

using System;

using System.IO;

using System.Net.Http;

using System.Text.RegularExpressions;

using System.Threading.Tasks;

using SixLabors.ImageSharp;

using SixLabors.ImageSharp.Processing;

using SixLabors.ImageSharp.Formats.WebP;


class Program

{

    static async Task Main(string[] args)

    {

        string imageUrl = "https://example.com/image.jpg"; // Replace with your image URL

        string title = "FolderName"; // Replace with your folder name

        int i = 1; // Replace with your image number

        

        string fpattern = "[^a-zA-Z_]+";

        string replacement = " ";


        string folderPath = Path.Combine("E:\\Imagesfolder", title);

        Directory.CreateDirectory(folderPath);


        HttpResponseMessage response = await client.GetAsync(imageUrl);

        response.EnsureSuccessStatusCode();

        byte[] imageBytes = await response.Content.ReadAsByteArrayAsync();

        

        string imageName = Regex.Replace(Path.GetFileNameWithoutExtension(new Uri(imageUrl).LocalPath), fpattern, replacement).Trim();

        string magen = title + " " + i;

        string imagePath = Path.Combine(folderPath, magen + ".webp");


        using (Image image = Image.Load(imageBytes))

        {

            // Apply compression or other optimizations here if needed

            image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2)); 

        // Example: Resize the image to half its original size


            // Save the image as .webp

            using (FileStream outputStream = File.Create(imagePath))

            {

                image.Save(outputStream, new WebPEncoder());

            }

        }

    }

}

```


Make sure to replace `"https://example.com/image.jpg"`, `"FolderName"`, and `1` with appropriate values.


In this code:


- `SixLabors.ImageSharp` is used to load, process, and save images.

- `client` is an instance of `HttpClient` that you need to initialize somewhere in your code.

- I've added resizing as an example of optimization. You can apply further optimizations as needed.

- `WebPEncoder` is used to encode and save the image in .webp format.

- The image is saved to the specified folder with the specified filename.


Remember to install the `ImageSharp` NuGet package if you haven't already done so. You can install it via the NuGet Package Manager or via the Package Manager Console with the following command:


```

Install-Package SixLabors.ImageSharp

```





Post a Comment

Previous Post Next Post