【问题标题】:Adaptive cards - serving images in bytes自适应卡片 - 以字节为单位提供图像
【发布时间】:2019-04-13 09:25:49
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你的文件有多大?即像素大小?
  • 不幸的是,它有点大 - 1920x1080。但我无法影响图像的大小,因为它来自外部服务。
  • 可能是这样,但在将图像写入卡之前注入一些逻辑来调整图像大小可能是值得的。这可能会有所帮助...stackoverflow.com/questions/1922040/…
  • 谢谢。我已经通过调整大小解决了问题。但是,我不认为这是最好的方法,因为我需要根据每个请求调整大量图像的大小。
  • 不,你是对的,但如果 1) 你必须使用该服务中的那些图像,并且 2) 你无法控制该服务,这听起来你有点手足无措。

标签: c# botframework adaptive-cards


【解决方案1】:

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

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

解决方法: 定义以下类,扩展 AdaptiveImage,添加 LongUrl 属性(写入 JSON 中相同的 url 属性)。

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

那么,在分配长网址时使用您的新图像类和新属性!

// 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"
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多