Telegram Bot C# - 发送对象位图

Telegram Bot C# - Send object Bitmap

我必须 "generate" 一个 png 文件并通过 SeendDocumentAsync 的 SendPhotoAsync 将其发送到 Telegram 机器人。

这是我的一段 C# 代码:

...
Bitmap speedometer = new Bitmap(@"C:\Immagini\bot\speedometer.png");
Bitmap pointer = new Bitmap(@"C:\Immagini\bot\pointer.png");
Bitmap finalImage = new Bitmap(speedometer);
using (Graphics graphics = Graphics.FromImage(finalImage))
{
    Bitmap rotatedPointer = RotateImage(pointer, efficienza_int * (float)1.8);
    rotatedPointer.MakeTransparent(Color.White);
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.DrawImage(rotatedPointer, 0, 0);
    ?????????????
}

现在,我想发送我的 finalImage 而不用 Save 方法将它保存在磁盘上。 我该怎么办?

多谢指教!

来自 Telegram Bot API 文档(link

正在发送文件 发送文件(照片、贴纸、音频、媒体等)的三种方式:

...

  1. Post 文件使用 multipart/form-data 以通常的方式通过浏览器上传文件。照片最大 10 MB,其他文件最大 50 MB。

你的问题不清楚! 但是,(如果我理解你的问题的话) 您正在使用此存储库中的 TelgramBotClient:https://github.com/TelegramBots

当您从此客户端调用 SendPhotoAsync 时,它会将 FileToSend 作为参数,代表您处理过旋转、透明度和平滑处理的照片。

当您传递此 FileToSend 时,您可以通过从处理后创建的临时文件加载照片来设置照片,或者您可以像这样从 MemoryStream 加载它的目录:

using System.Drawing;
using System.Drawing.Drawing2D;
using Telegram.Bot;
using Telegram.Bot.Args;
using System.IO;
using System.Drawing.Imaging;

namespace LoadGraphicsFromMemory
{
    public static class ImageExtensions
    {
        public static MemoryStream ToMemoryStream(this Bitmap image, ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, format);
                return ms;
            }
        }

    }

    class Program
    {
        private static float efficienza_int;
        private static readonly TelegramBotClient Bot = new TelegramBotClient("Your API key");

        static void Main(string[] args)
        {


            Bot.OnMessage += BotOnMessageReceived;
        }

        private static void BotOnMessageReceived(object sender, MessageEventArgs e)
        {
            Bitmap speedometer = new Bitmap(@"C:\Immagini\bot\speedometer.png");
            Bitmap pointer = new Bitmap(@"C:\Immagini\bot\pointer.png");
            Bitmap finalImage = new Bitmap(speedometer);
            using (Graphics graphics = Graphics.FromImage(finalImage))
            {
                Bitmap rotatedPointer = RotateImage(pointer, efficienza_int * (float)1.8);
                rotatedPointer.MakeTransparent(Color.White);
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.DrawImage(rotatedPointer, 0, 0);

            }

            Bot.SendPhotoAsync(e.Message.Chat.Id, new Telegram.Bot.Types.FileToSend("My File", finalImage.ToMemoryStream(ImageFormat.Jpeg)));
        }

        private static Bitmap RotateImage(Bitmap pointer, object p)
        {
            return pointer;
        }


    }
}

将其保存到 MemoryStream,然后在您的呼叫中将 MemoryStream 发送给机器人,如下所示:

using (MemoryStream ms = new MemoryStream())
using (Bitmap finalImage = new Bitmap(speedometer))
{
    using (Graphics graphics = Graphics.FromImage(finalImage))
    {
        // ... stuff
    }
    finalImage.Save(ms, ImageFormat.Png);
    // This is important: otherwise anything reading the stream
    // will start at the point AFTER the written image.
    ms.Position = 0;
    Bot.SendPhotoAsync(/* send 'ms' here. Whatever the exact args are */);
}

异步发送可能需要流保持打开状态。虽然,通常情况下,当您有这样的异步发送时,您可以指定一个函数,该函数应在发送完成后调用。

在那种情况下,您应该MemoryStream放在using块中,而是将流对象存储在您的全局变量中class,并确保处理异步发送结束的函数处理它。

还要注意这个问题...

bot.sendphoto does not work asp.net

显然 SendPhotoAsync 不足以实际发送;那里的答案指定您需要调用 .GetAwaiter().GetResult()。我不知道 API,所以你必须自己弄清楚。