how to convert jpg to webp in C#
Asked Answered
S

3

9

I'm writing a telegram bot that takes jpg from it's users and sends it back as stickers. I did this correctly by downloading jpg, change the extension of file to png and upload and send it back as a sticker message to the user. as shown below:

var file = await bot.GetFileAsync(update.Message.Photo.LastOrDefault()?.FileId);
var filename = file.FileId + "." + file.FilePath.Split('.').Last();
var pngFileName = filename.Split('.')[0] + ".png";
using (var saveImageStream = System.IO.File.Open(pngFileName, FileMode.Create))
{
    await bot.DownloadFileAsync(file.FilePath, saveImageStream);
    await bot.SendTextMessageAsync(update.Message.Chat.Id, "please wait...");
}
using (var stream = System.IO.File.Open(pngFileName, FileMode.Open))
{
    await bot.SendStickerAsync(update.Message.Chat.Id, stream);
}

but the these stickers don't load in telegram on IOS devices and this code just works for telegram users on android. I tried to just changing the extension of jpg file to webp but it didn't work.

after that I downloaded the standard telegram stickers and found that the standard format of stickers in telegram is webp files. now I want to know how can I convert received jpg file to webp file.

I searched alot and just find this , found here .

using (Image image = Image.FromFile("image.jpg"))
{
    Bitmap bitmap = new Bitmap(image);
    WebPFormat.SaveToFile("image.webp", bitmap);
}

I added it's files to my project and I added "using LibwebpSharp;" at the top of my code, but when I add it's sample code, the VS cannot find "WebpFormat" class.

please help me and answer my question: "How can I convert jpg to webp in C# telegram bot?" thank you

Schnurr answered 10/1, 2019 at 20:7 Comment(2)
Your sample code is for the closed CorePlex project webp for .Net, and all CodePlex projects are archived and no longer updated. You could download the archive, or switch to the newer forked project from gibhub libwebp-net.Reduction
thank you. that was a good hintSchnurr
S
13

I solved this problem in this way:

  1. I installed Imazen.WebP nuget.
  2. I downloaded the 32bit dll from here and added it to release folder.
  3. I added using Imazen.WebP;in top of my code
  4. I used this code to convert jpg to webp.
var file = await bot.GetFileAsync(update.Message.Photo.LastOrDefault()?.FileId);
var jpgFileName = file.FileId + ".jpg";
using (var saveImageStream = System.IO.File.Open(jpgFileName,FileMode.Create))
{
    await bot.DownloadFileAsync(file.FilePath, saveImageStream);
    await bot.SendTextMessageAsync(update.Message.Chat.Id, "please wait...");
}

var webpFileName = file.FileId + ".webp";
using (Bitmap bitmap = new Bitmap(jpgFileName))
{
    using (var saveImageStream = System.IO.File.Open(webpFileName, FileMode.Create))
    {
        var encoder = new SimpleEncoder();
        encoder.Encode(bitmap, saveImageStream, 20);
    }
}

using (var stream = System.IO.File.Open(webpFileName, FileMode.Open))
{
    await bot.SendStickerAsync(update.Message.Chat.Id, stream);
}

System.IO.File.Delete(jpgFileName);
System.IO.File.Delete(webpFileName);
Schnurr answered 11/1, 2019 at 17:14 Comment(1)
It is 2022. Is this still the best way @PersianLionKing. I don't like the idea of downloading a .dll and adding it to the release folder.Henotheism
C
5

Install the following packages first using Visual Studio's NuGet package manager:

Install-Package System.Drawing.Common
Install-Package ImageProcessor
Install-Package ImageProcessor.Plugins.WebP 

Then use this code for conversion:

using (var webPFileStream = new FileStream(WebpFilePath, FileMode.Create))
{
    using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
    {

        imageFactory.Load(File.OpenRead(OriginalImagePath))
                    .Format(new WebPFormat())
                    .Quality(100)
                    .Save(webPFileStream);
    }
}
Colorless answered 3/8, 2022 at 11:38 Comment(0)
G
-2

Imageprocessor looks like a good library to convert the image. There is a Webp plugin.

Here is an article that may help.

Code example:

using ImageProcessor;
using ImageProcessor.Plugins.WebP.Imaging.Formats;

using (var normalFileStream = new FileStream(normalImagePath, FileMode.Create))
using (var webPFileStream = new FileStream(webPImagePath, FileMode.Create))
using (var imageFactory = new ImageFactory(preserveExifData: false))
{
    imageFactory.Load(image.OpenReadStream())
                           .Format(new WebPFormat())
                           .Quality(50)
                           .Save(webPFileStream);
}
Gainer answered 27/6, 2022 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.