Send an image rather than a link

后端 未结 2 1278
有刺的猬
有刺的猬 2020-12-06 06:32

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

相关标签:
2条回答
  • 2020-12-06 07:23

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-06 07:32

    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);
    
    0 讨论(0)
提交回复
热议问题