Send an image rather than a link

别来无恙 提交于 2019-11-27 23:06:22
Jim Lewallen

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);

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