电报机器人C#-发送对象位图
我必须“生成”一个png文件,并通过SeendDocumentAsync的SendPhotoAsync将其发送到电报机器人 这是我的一段C代码: 现在,我想发送finalImage,但不使用Save方法将其保存在磁盘上。 我该怎么办电报机器人C#-发送对象位图,c#,image,bitmap,send,telegram-bot,C#,Image,Bitmap,Send,Telegram Bot,我必须“生成”一个png文件,并通过SeendDocumentAsync的SendPhotoAsync将其发送到电报机器人 这是我的一段C代码: 现在,我想发送finalImage,但不使用Save方法将其保存在磁盘上。 我该怎么办 谢谢你的建议 来自电报机器人API文档() 发送文件 发送文件有三种方式(照片、贴纸、音频、媒体等): 以通过浏览器上载文件的通常方式,使用多部分/表单数据发布文件。照片的最大大小为10 MB,其他文件的最大大小为50 MB 你的问题不清楚! 但是,(如果我正确理
谢谢你的建议 来自电报机器人API文档() 发送文件 发送文件有三种方式(照片、贴纸、音频、媒体等):
你的问题不清楚! 但是,(如果我正确理解你的问题) 您正在使用此存储库中的TelgramBotClient: 从该客户端调用SendPhotoAsync时,它将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;
}
}
}
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
,并将调用中的MemoryStream
发送到bot,如下所示:
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;
}
}
}
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
放入中,而应将流对象存储在类中的全局变量中,并确保处理异步发送结束的函数处理它
也请注意这个问题
显然,SendPhotoAsync
不足以实际发送它;那里的答案指定您需要调用.GetAwaiter()
和.GetResult()
。我不知道API,所以你必须自己解决。谢谢,你说到点子上了!我正在尝试您的解决方案,但使用命令SendPhotoAsync,我正在处理异常:ObjectDisposedException。这个答案不起作用。您不能返回使用
块在中创建的流-流从离开ToMemoryStream
函数的那一刻起将被释放,因此无效(因此ObjectDisposedException
)。当人们只是说答案无效时,不要要求他们接受。