如何在C#中将jpg转换为webp [英] how to convert jpg to webp in C#

查看:1247
本文介绍了如何在C#中将jpg转换为webp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个电报机器人,该机器人从用户那里获取jpg并将其作为贴纸发送回去. 我通过下载jpg,将文件的扩展名更改为png并上传并作为不干胶标签消息发回给用户的方式正确地做到了.如下图所示:

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);
}

但是这些标签不会在IOS设备上的电报中加载,并且此代码仅适用于android上的电报用户.我试图只是将jpg文件的扩展名更改为webp,但没有用.

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.

此后,我下载了标准电报贴纸,发现电报中贴纸的标准格式是webp文件. 现在我想知道如何将收到的jpg文件转换为webp文件.

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);
}

我将其文件添加到我的项目中,并添加了使用LibwebpSharp;".在我的代码的顶部,但是当我添加它的示例代码时,VS无法找到"WebpFormat"类.

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.

请帮助我并回答我的问题: 如何在C#电报bot中将jpg转换为webp?" 谢谢

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

推荐答案

以这种方式解决的问题

1)我安装了 Imazen.WebP nuget. 2)我从此处下载了32位dll,并将其添加到release文件夹中. 3)我添加了使用Imazen.WebP; 在我的代码顶部4)我使用此代码将jpg转换为webp.

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);

它正常工作

这篇关于如何在C#中将jpg转换为webp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆