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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Persian LionKing
  • 304
  • 1
  • 5
  • 19
  • 1
    Your sample code is for the closed CorePlex project [webp for .Net](https://archive.codeplex.com/?p=webp), 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](https://github.com/imazen/libwebp-net). – NetMage Jan 10 '19 at 22:22
  • thank you. that was a good hint – Persian LionKing Jan 11 '19 at 17:02

3 Answers3

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);
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
Persian LionKing
  • 304
  • 1
  • 5
  • 19
  • 6
    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. – J86 Jan 20 '22 at 11:07
4

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);
    }
}
Naresh Bisht
  • 645
  • 6
  • 16
-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);
}
Ebe
  • 1
  • 2