how to convert jpg to webp in C#

前端 未结 1 1240
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 08:01

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 u

相关标签:
1条回答
  • 2021-01-15 08:35

    this problem solved 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);
    

    and it worked correctly

    0 讨论(0)
提交回复
热议问题