Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert jpg to webp in C#

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

like image 870
Persian LionKing Avatar asked Jan 01 '23 10:01

Persian LionKing


2 Answers

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);
like image 64
Persian LionKing Avatar answered Jan 05 '23 19:01

Persian LionKing


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);
    }
}
like image 45
Naresh Bisht Avatar answered Jan 05 '23 19:01

Naresh Bisht