Send an image rather than a link
Asked Answered
P

2

9

I'm using the Microsoft Bot Framework with Cognitive Services to generate images from a source image that the user uploads via the bot. I'm using C#.

The Cognitive Services API returns a byte[] or a Stream representing the treated image.

How can I send that image directly to my user? All the docs and samples seem to point to me having to host the image as a publically addressable URL and send a link. I can do this but I'd rather not.

Does anyone know how to simple return the image, kind of like the Caption Bot does?

Plosive answered 31/8, 2016 at 9:45 Comment(0)
N
9

You should be able to use something like this:

var message = activity.CreateReply("");
message.Type = "message";

message.Attachments = new List<Attachment>();
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("https://placeholdit.imgix.net/~text?txtsize=35&txt=image-data&w=120&h=120");
string url = "data:image/png;base64," + Convert.ToBase64String(imageBytes)
message.Attachments.Add(new Attachment { ContentUrl = url, ContentType = "image/png" });
await _client.Conversations.ReplyToActivityAsync(message);
Newmark answered 2/9, 2016 at 22:46 Comment(2)
This is working in Web App Bot, it's throws error in Microsoft teams...Kunming
it would be great if someone explain what does that mean by this line -- string url = "data:image/png;base64," + Convert.ToBase64String(imageBytes) how does this fix issue which OP asked.Mcneil
D
4

The image source of HTML image elements can be a data URI that contains the image directly rather than a URL for downloading the image. The following overloaded functions will take any valid image and encode it as a JPEG data URI string that may be provided directly to the src property of HTML elements to display the image. If you know ahead of time the format of the image returned, then you might be able to save some processing by not re-encoding the image as JPEG by just returning the image encoded as base 64 with the appropriate image data URI prefix.

    public string ImageToBase64(System.IO.Stream stream)
{
    // Create bitmap from stream
    using (System.Drawing.Bitmap bitmap = System.Drawing.Bitmap.FromStream(stream) as System.Drawing.Bitmap)
    {
        // Save to memory stream as jpeg to set known format.  Could also use PNG with changes to bitmap save 
        // and returned data prefix below
        byte[] outputBytes = null;
        using (System.IO.MemoryStream outputStream = new System.IO.MemoryStream())
        {
            bitmap.Save(outputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            outputBytes = outputStream.ToArray();
        }

        // Encoded image byte array and prepend proper prefix for image data. Result can be used as HTML image source directly
        string output = string.Format("data:image/jpeg;base64,{0}", Convert.ToBase64String(outputBytes));

        return output;
    }
}

public string ImageToBase64(byte[] bytes)
{
    using (System.IO.MemoryStream inputStream = new System.IO.MemoryStream())
    {
        inputStream.Write(bytes, 0, bytes.Length);
        return ImageToBase64(inputStream);
    }
}
Dentiform answered 18/10, 2016 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.