botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] Hi-def image is too small when viewed in WebChat. How to open a popup or new tab by clicking it?

Open luisnatividade opened this issue 8 years ago • 17 comments

Bot Info

SDK Platform: .NET Active Channels: Web Chat Deployment Environment: Azure Bot Service

Issue Description

Image too small. Impossible to see the content.

Code Example

          var replyMessage = context.MakeMessage();
          Attachment attachment = new Attachment();  
         attachment.ContentType = "image/png";
         var imageData = Convert.ToBase64String(File.ReadAllBytes(HttpContext.Current.Server.MapPath($"~/images/{path}")));
          attachment.ContentUrl = $"data:image/png;base64,{imageData}";
          replyMessage.Attachments.Add(attachment);
          await context.PostAsync(replyMessage);

Expected Behavior

Open a popup or a new tab with the image, after a user click.

Actual Results

image

The image is too small and user cannot understand the content of it. Is there any way to open the image in a popup or newtab after the click of the user?

luisnatividade avatar Jan 12 '18 11:01 luisnatividade

@luisnatividade Put your image in a Hero Card with a button that links to the full resolution image URL.

nwhitmont avatar Jan 12 '18 17:01 nwhitmont

As he said using a hero card you can accomplish this. Here is a code example:

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            IMessageActivity reply = context.MakeMessage();

            HeroCard heroCard = new HeroCard()
            {
                Images = new List<CardImage>
                {
                    new CardImage(url: $"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png")
                },
                Tap = new CardAction()
                {
                    Value = $"https://www.google.com",
                    Type = "openUrl",
                }
            };

            reply.Attachments = new List<Attachment>
            {
                heroCard.ToAttachment()
            };
            await context.PostAsync(reply);
        }

JasonSowers avatar Jan 12 '18 17:01 JasonSowers

@nwhitmont and @JasonSowers Thank you. Does it work with local images?

luisnatividade avatar Jan 12 '18 17:01 luisnatividade

It should work with local ones as well. I do not have an example of one already coded up unfortunately

JasonSowers avatar Jan 12 '18 20:01 JasonSowers

I just tried it out and the following code worked for me:

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {

            var reply2 = await result as Activity;
            Activity reply = reply2.CreateReply();
            string base64String;
            string image64;
            try
            {
                using (Image image = Image.FromFile(@"C:\source\bomb300.png"))
                {
                    using (MemoryStream m = new MemoryStream())
                    {
                        image.Save(m, image.RawFormat);
                        byte[] imageBytes = m.ToArray();

                        // Convert byte[] to Base64 String
                        base64String = Convert.ToBase64String(imageBytes);
                    }
                }
                image64 = "data:image/png;base64," + base64String;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            HeroCard heroCard = new HeroCard()
            {
                Images = new List<CardImage>
                {
                    new CardImage(
                        url: image64)
                },
                Tap = new CardAction()
                {
                    Value = $"https://www.google.com",
                    Type = "openUrl",
                }
            };

            reply.Attachments = new List<Attachment>
            {
                heroCard.ToAttachment()
            };
            await context.PostAsync(reply);
        }

JasonSowers avatar Jan 12 '18 21:01 JasonSowers

Closing this issue as it should be resolved for you now. Feel free to continue the conversation if needed.

JasonSowers avatar Jan 12 '18 22:01 JasonSowers

@JasonSowers Thanks for the answer! The only thing is that I dont't want to open an url when clicking in the image. I want to open a tab with the image. Is it possible with local images?

I want to do this because since the width of the chatbot is small it's difficult to see clearly the image.

Thanks in advance.

luisnatividade avatar Jan 16 '18 18:01 luisnatividade

I missed that part in my example. The easiest way to do this would be to add the picture to your project and call it as an endpoint like Value = @"http://localhost:3979/bomb300.png", here is this code again with minor changes that should be what you need:

            var reply2 = await result as Activity;
            Activity reply = reply2.CreateReply();
            string base64String;
            string image64;
            try
            {
                using (Image image = Image.FromFile(@"C:\Users\v-jassow\Documents\Visual Studio 2017\Projects\Bot Application6\Bot Application6\bomb300.png"))
                {
                    using (MemoryStream m = new MemoryStream())
                    {
                        image.Save(m, image.RawFormat);
                        byte[] imageBytes = m.ToArray();

                        // Convert byte[] to Base64 String
                        base64String = Convert.ToBase64String(imageBytes);
                    }
                }
                image64 = "data:image/png;base64," + base64String;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            HeroCard heroCard = new HeroCard()
            {
                Images = new List<CardImage>
                {
                    new CardImage(
                        url: image64)
                },
                Tap = new CardAction()
                {
                    Value = @"http://localhost:3979/bomb300.png",
                    Type = "openUrl",
                }
            };

            reply.Attachments = new List<Attachment>
            {
                heroCard.ToAttachment()
            };
            await context.PostAsync(reply);

JasonSowers avatar Jan 16 '18 23:01 JasonSowers

@JasonSowers I'm working with the App Service Editor on Azure Portal and I have the image on "WWWROOT" folder. The way I get the image is using HttpContext.Current.Server.MapPath($"~/{imageName}"). I'm testing the chatbot using the webchat channel. So I think using the localhost won't work. The code of the previous answer works, except the line: Value = @"http://localhost:3979/bomb300.png". I tried different ways but sill can't make it to work...

luisnatividade avatar Jan 17 '18 14:01 luisnatividade

I will have to try this out myself in azure bot service, I will try to get to this today.

JasonSowers avatar Jan 17 '18 21:01 JasonSowers

@JasonSowers Any update? I tried different ways today but I'm still stuck in the problem...

luisnatividade avatar Jan 18 '18 22:01 luisnatividade

I will look today

JasonSowers avatar Jan 19 '18 16:01 JasonSowers

I want to make sure I recreate this properly is this a web app bot in azure?

JasonSowers avatar Jan 19 '18 17:01 JasonSowers

@JasonSowers yes it is.

luisnatividade avatar Jan 19 '18 18:01 luisnatividade

@luisnatividade I have successfully accomplished what you need, Here is the project https://github.com/JasonSowers/HeroCardOpenUrl

JasonSowers avatar Jan 19 '18 21:01 JasonSowers

Thanks @JasonSowers it works perfectly in my project. I was missing the Uri...

luisnatividade avatar Jan 19 '18 21:01 luisnatividade

Glad it works for you now.

JasonSowers avatar Jan 19 '18 23:01 JasonSowers