0

I am working on embed image in HTML and send the html as email. I have a existing library to get the html as string. The library will parse the string and will handling the email part. So what I want to do is to convert the image to string and embed it in the html string. What I should do? iTextSharp?

DennisL
  • 293
  • 1
  • 3
  • 12
  • Why not simply use ``? – Obsidian Age Nov 27 '17 at 03:44
  • I need to dynamically generate am image with data returned, and sent the chart to the others through email in html. I know this librarySystem.Web.UI.DataVisualization.Charting can generate chart and work with memory stream. But I am not sure how to embed the chart in html. – DennisL Nov 27 '17 at 03:48
  • 1
    Possible duplicate of [How to embed images in email](https://stackoverflow.com/questions/4312687/how-to-embed-images-in-email) – Obsidian Age Nov 27 '17 at 03:51
  • If you have Image, then you can save it on Host Server and the Online link of your Image (like as http:\\MyDomain.com\images\myimage.jpg) use in your html of mail using "> – Durgesh Pandey Nov 27 '17 at 03:53
  • Thanks. But that's something I am trying to avoid, as we need to deal with storage every time we run this functions. If this function is used many times, then a lot of rubbish will be left in the our azure storage – DennisL Nov 27 '17 at 03:56

1 Answers1

2

You can convert image to base64 encode and embedded in <img src=''>

    System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
    Image1.ImageUrl = "data:image/png;base64," + base64String;
    Image1.Visible = true;

Result example:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACR0lEQVRYha1XvU4bQRD+bF/JjzEnpUDwCPROywPgB4h0PUWkFEkLposUIYyEU4N5AEpewnkDCiQcjBQpWLiLjk3DrnZnZ3buTv4ae25mZ+Z2Zr7daxljDGpg++Mv978Y5Nhc6+Di5tk9u7/bR3cjY9eOJnMUh3mg5y0roBjk+PF1F+1WCwCCJKTgpz9/ozjMg+ftVQQ/PtrB508f1OAcau8ADW5xfLRTOzgAZMPxTNy+YpDj6vaPGtxPgvpL7QwAtKXts8GqBveT8P1p5YF5x8nlo+n1p6bXn5ov3x9M+fZmjDGRXBXWH5X/Lv4FdqCLaLAmwX1/VKYJtIwJeYDO+dm3PSePJnO8vJbJhqN62hOUJ8QpoD1Au5kmIentr9TobAK04RyJEOazzjV9KokogVRwjvm6652kniYRJUBrTkft5bUEAGyuddzz7noHALBYls5O09skaE+4HdAYruobUz1FVI6qcy7xRFW95A915pzjiTp6zj7za6fB1lay1/Ssfa8/jRiLw/n1k9tizl7TS/aZ3xDakdqUByR/gDcF0qJV8QAXHACy+7v9wGA4ngWLVskDo8kcg4Ot8FpGa8PV0I7MyeWjq53f7Zrer3nyOLYJpJJowgN+g9IExNNQ4vLFskwyJtVrd8JoB7g3b4rz66dIpv7UHqg611xw/0om8QT7XXBx84zheCbKGui2U9n3p/YAlSVyqRqc+kt+mCyWJTSeoMGjOQciOQDXA6kjVTsL6JhpYHtA+wihPaGOWgLqnVACPQua4j8NK7bPLP4+qQAAAABJRU5ErkJggg==" width="32" height="32">
Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43