2017-10-25 2 views
2

Ich möchte ein QR-Code-Bild zurück zum Client anzeigen.Antwort QR-Code auf Bot-Framework mit MessagingToolkit.QRCode

Hier ist mein Code in 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); 
} 

Ich bin nicht wirklich sicher, wie wenn ein Bildobjekt als Anlage antworten ..

Thank you very much!

+0

Mögliches Duplikat von [Ein Bild anstelle eines Links senden] (https://stackoverflow.com/questions/39246174/send-an-image-rather-than-a-link) –

Antwort

2

Sie brauchen nur ein paar Schritte zwischen QRCode encode und Anlageninhalt: wandeln die Bitmap zu einem byte[], dann Base64 konvertieren und fügen Sie es als 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:

Demo