问题
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