【问题标题】:Reply QR Code on Bot framework with MessagingToolkit.QRCode使用 MessagingToolkit.QRCode 回复 Bot 框架上的二维码
【发布时间】:2017-10-25 17:33:38
【问题描述】:
我想向客户端显示一个二维码图片。
这是我在 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);
}
我不太确定如何将图像对象作为附件回复..
非常感谢!
【问题讨论】:
标签:
c#
botframework
qr-code
【解决方案1】:
您只需要在您的二维码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);
演示: