hero card image attachment not loading

谁说我不能喝 提交于 2020-01-04 05:34:13

问题


I am using the code

private static Attachment HeroCard() {
   var hc = new HeroCard {
      Images=new List<CardImage> {
         new CardImage(@"C:\Users\.....\imgs\testImage.jpg") }
      };
    return hc.ToAttachment();
}

To load an image in a hero card's attachment. This works fine but if I try to use the local folder instead e.x

@"~\imgs\testImage.jpg"

The image fails to load. I have tried different other formats regarding the path with no success. What am I missing?


回答1:


try to use the local folder @"~\imgs\testImage.jpg"

It seems that your image files are stored in your project imgs folder and you’d like to send the image to client. You can try to use System.Web.HttpContext.Current.Server.MapPath to read MapPath. The following code works for me, please refer to it.

var hc = new HeroCard
{
    Images = new List<CardImage> {
    new CardImage(System.Web.HttpContext.Current.Server.MapPath(@"~\imgs\testImage.jpg"))
}
};



回答2:


Down below code work on MS Bot Framework SDK v4

var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\testImage.png");
var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));

var heroCard = new ThumbnailCard
{
    Images = new List<CardImage> { new CardImage($"data:image/png;base64,{imageData}") },
};

You can find more examples about working with attachments here



来源:https://stackoverflow.com/questions/49347786/hero-card-image-attachment-not-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!