如何在Microsoft bot框架中仅显示图像的base64编码字符串显示图像?

问题描述:

我尝试了以下代码,这是我在模拟器中获得的输出

i tried the below code, and here is the output i get in emulator

message.Attachments.Add(new Attachment() { ContentUrl = $"data:image/jpeg;base64,xxxx" });

message.Attachments.Add(new Attachment() { ContentUrl = $"data:image/jpeg;base64,xxxx" });

数据uri图像似乎有一个最大大小,但是您的初始代码对我来说不错,并且不会引发显式的内部服务器错误(因为它如果datauri太大,则可以).

There appears to be a max size for data uri images, however your initial code looks good to me and isn't throwing an explicit internal server error (as it would if the datauri is too large).

我已经实现了类似的东西:

I've implemented something similar:

var reply = message.CreateReply("Here's a **datauri image attachment**");
reply.Attachments = new List<Attachment> {
    new Attachment()
    {
        ContentUrl = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAAQABADAREAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAACAUH/8QAJhAAAQMDAwQCAwAAAAAAAAAAAQIDBQQGEQcIEgATISIUMRUjUf/EABYBAQEBAAAAAAAAAAAAAAAAAAMBBP/EAB8RAAICAQQDAAAAAAAAAAAAAAECAAMRBBITIiFB8P/aAAwDAQACEQMRAD8AubjdVbtj5cQFi3tX2lS/ka16Rko9pZqHHfklplgKAylJPNR/vEZPWyvTpUN7jMyK3M21fE03ZLuQ1Gmbyc0j1Dudq7o8RztXFzXEGtacZeQhxipKT7D9qcKUOQ+skfRWKrdqxj71HI4erHME97633Fc+pF10c64pIg7ll6CldoEcHEoTVL7fMZ9se2CPOekdkCiSjIYmLvYvMRdLQPXDG3FGSEzK1iKB4rYCnaan7oVwcCQCHVqGTkkeEefGOgbTtjccyW6sM4QAT//Z",
        ContentType = "image/jpg",
        Name = "datauri"
    }
};

这会导致模拟器显示此图像(我需要更多代表才能嵌入图像.. ugh ..)

Which results in the emulator showing this image (I need more rep to embed images.. ugh..)

更新:〜20kb图像的数据uri版本工作正常,但是〜140kb图像的数据uri版本失败,模拟器中出现"500 internalservererror".猜猜毕竟有大小限制.

Update: a data uri version of a ~20kb image works just fine, however a data uri version of a ~140kb image fails with a "500 internalservererror" in the emulator. Guess there is a size limit after all..

这样,您可以验证您使用的datauri是有效的图像吗?您可以使用img元素创建一个简单的html页面,将其粘贴到ContentUrl中的值中,然后在html页面中查看图像吗?甚至只是将其粘贴到浏览器地址栏中.

As such, can you validate that he datauri you're using is a valid image? Can you create a simple html page with an img element, paste in the value in your ContentUrl and see the image in the html page? Or even just paste it into a browser address bar.