gpt4 book ai didi

c# - 使用 MessagingToolkit.QRCode 在 Bot 框架上回复二维码

转载 作者:行者123 更新时间:2023-11-30 21:37:46 32 4
gpt4 key购买 nike

我想给客户端显示一个二维码图片。

这是我在 RootDialog 中的代码,

[LuisIntent("None")]
[LuisIntent("")]
public async Task None(IDialogContext context, LuisResult result)
{
string qrText = "Photo";
QRCodeEncoder enc = new QRCodeEncoder();
Bitmap qrcode = enc.Encode(qrText);

var message = context.MakeMessage();
Attachment attachment = new Attachment();
attachment.ContentType = "image/jpg";
attachment.Content = qrcode as Image; // This line is not sure...
attachment.Name = "Image";
message.Attachments.Add(attachment);
await context.PostAsync(message);
}

虽然我不太确定如何将图像对象作为附件回复..

非常感谢!

最佳答案

您只需要在 QRCode encode 和附件内容之间进行几个步骤:将 Bitmap 转换为 byte[],然后转换到 base64 并将其添加为 ContentUrl:

string qrText = "Photo";
QRCodeEncoder enc = new QRCodeEncoder();
Bitmap qrcode = enc.Encode(qrText);

// Convert the Bitmap to byte[]
System.IO.MemoryStream stream = new System.IO.MemoryStream();
qrcode.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = stream.ToArray();

var message = context.MakeMessage();
Attachment attachment = new Attachment
{
ContentType = "image/jpg",
ContentUrl = "data:image/jpg;base64," + Convert.ToBase64String(imageBytes),
Name = "Image.jpg"
};
message.Attachments.Add(attachment);
await context.PostAsync(message);

演示:

Demo

关于c# - 使用 MessagingToolkit.QRCode 在 Bot 框架上回复二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46923111/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com