如何避免图像 MS Bot 构建器框架的自动重新调用

How to avoid automatic resclaling of image MS Bot builder framework

你好,我正在编写一个聊天机器人,我需要能够在聊天中发送图像。它们只是小图标。 我已尝试调整 "handling attachtments" 示例 (https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/15.handling-attachments/Bots/AttachmentsBot.cs)

中的代码

还有文档的这一页:(https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-add-media-attachments?view=azure-bot-service-4.0&tabs=csharp)

但它会自动调整小图标的大小以适应更大的框架。我不确定为什么...

请看这张解释问题的截图:

这是实际图像: !!

这是我用过的代码:

var reply = MessageFactory.Text("This is an inline attachment.");
reply.Attachments = new List<Attachment>() { GetInlineAttachment() };
await stepContext.Context.SendActivityAsync(reply, cancellationToken);
       private static Attachment GetInlineAttachment()
        {
            var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\uc2icon.png");
            var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));

            return new Attachment
            {
                Name = @"Resources\architecture-resize.png",
                ContentType = "image/png",
                ContentUrl = $"data:image/png;base64,{imageData}",
            };
        }

我是 c# 的新手,一般来说也是编码,非常感谢您的帮助!!谢谢

您可以使用 Adaptive Card Attachment 来解决这个问题,试试下面的代码:

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{

    var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\uc2icon.png");
    var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));
    var url = $"data:image/png;base64,{imageData}";

    var adaptiveJsonString = "{\"$schema\":\"http://adaptivecards.io/schemas/adaptive-card.json\",\"type\":\"AdaptiveCard\",\"version\":\"1.0\",\"body\":[{\"type\":\"ImageSet\",\"imageSize\":\"auto\",\"images\":[{\"type\":\"Image\",\"url\":\""+ url + "\"}]}]}";

    var adaptiveCardAttachment = new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.adaptive",
        Content = JsonConvert.DeserializeObject(adaptiveJsonString),
    };


    await turnContext.SendActivityAsync(MessageFactory.Attachment(adaptiveCardAttachment), cancellationToken);
}

结果:

有关机器人自适应卡片的更多示例,see here。 希望它有所帮助,祝你有美好的一天。