自适应卡片 - 以字节为单位提供图像

Adaptive cards - serving images in bytes

我正在尝试以这种方式将图像放入 Bot 框架中的 Adaptive Card 中:

card.Body.Add(new AdaptiveImage()
{
    Type = "Image",
    Url = new Uri(pictureUrl),
    Size = AdaptiveImageSize.Large
});

它正在工作。问题出在 Url。我从外部 Web 服务获取 Base64 格式的图像。但有时我得到的图像太大,所以我得到 The uri string is too long 异常。

有什么办法可以解决这个问题吗?例如,启用以字节为单位将图像放入Adaptive card。

感谢您报告此问题。根本原因是 pictureUrl 比 .NET 的最大 Uri 长度长。我们 tracking fixing this here.

有一个非常简单的解决方法可用,因为限制出现在您用来简单制作卡片的 .NET C# 库中,但 WebChat 不使用 C# 库来显示卡片(它使用JS 库,并且 JS/HTML 没有长度限制!)。因此,在您的情况下唯一不起作用的是生成 JSON... 但有一个简单的修复方法!

解决方法: 定义下面的 class,扩展 AdaptiveImage,添加一个 LongUrl 属性(它写入 [=31= 中相同的 url 属性 ]).

public class AdaptiveImageWithLongUrl : AdaptiveImage
{
    [JsonProperty(PropertyName = "url", Required = Required.Always)]
    public string LongUrl { get; set; }
}

然后,在指定长网址时使用新图像 class 和新图像 属性!

// A data URL that's longer than .NET max length
string actualUrl = "data:image/gif;base64," + string.Join("", new int[120000].Select(i => "A")) + "end";

AdaptiveCard card = new AdaptiveCard("1.0")
{
    Body =
    {
        new AdaptiveImageWithLongUrl()
        {
            LongUrl = actualUrl
        }
    }
};

// Place the JObject in the attachment!
var attachment = new Attachment()
{
    Content = card,
    ContentType = "application/vnd.microsoft.card.adaptive",
    Name = "cardName"
};