0

I am unable to append Image in dynamic HTML. I am getting image in byte[] form, So I am trying to convert it into image and want to append it into HTML in string builder.

But I am not able to do so. My code is below

public StringBuilder getEmailBackGroundTemplate(string CompanyName, byte[] CompanyLogo, string CompanyURL, string ContactPersonName)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<table cellpadding='0' cellspacing='0' border='0' width='100%'>");
    sb.Append("<tr>");
    sb.Append("<td style='width: 10%;'></td>");
    sb.Append("<td width='75%'>");
    sb.Append("<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #00497c;'>");
    sb.Append("<tr>");
    sb.Append("<td><a href='" + CompanyURL + "'>");

    if (CompanyLogo != null && CompanyLogo.Length > 0)
    {
        //Mohan: if Company logo exist then Attach in email
        MemoryStream ms = new MemoryStream(CompanyLogo, 0 ,CompanyLogo.Length);
        ms.Write(CompanyLogo, 0, CompanyLogo.Length);
        Image LogoImage = Image.FromStream(ms, true);

        sb.Append("<img src='" + LogoImage + "' border='0' style='margin-top: 5px; width: 175px; height: 53px; margin-left: 5px; margin-bottom: 5px;'></a></td>");
    }

    sb.Append("<td ></td>");
    sb.Append("</tr>");
    sb.Append("</table>");

    return sb;
}

I am not able to append, please can anyone help me how I can append this without save as a file i want to append it into stringbuilder.

Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
Rakesh
  • 101
  • 2
  • 4
  • 13
  • This is Windows application and I am getting byte[] from service (web service) – Rakesh Apr 05 '18 at 09:38
  • 1
    some info on embedding images into html: https://stackoverflow.com/questions/1207190/embedding-base64-images – FrankM Apr 05 '18 at 09:39

2 Answers2

1

You can convert the byte array directly to a base64 string and use as a data URI, e.g

var imageString = string.Format("data:image/png;base64,{0}", Convert.ToBase64String(CompanyLogo));
sb.Append("<img src='" + imageString + "' border='0' style='margin-top: 5px; width: 175px; height: 53px; margin-left: 5px; margin-bottom: 5px;'></a></td>");
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
  • Thanks Chibueze Opata, This works fine for me. Thanks a lot – Rakesh Apr 05 '18 at 10:10
  • in gmail or outlook when I am going to attach this its not showing in gmail – Rakesh Apr 10 '18 at 04:38
  • Yes, you should have mentioned you were using in Gmail, [they don't support that anymore](https://stackoverflow.com/q/3279523/612717) You have to save to an online host and insert the url. – Chibueze Opata Apr 10 '18 at 09:36
  • there is no other option? without saving we can't do it? because they told me we don't want to save it in any folder on server so what they said save it as byte in sql then append it like you gave solution. so many changes I have to do, some customers are using smtp of gmail and some are using in outlook but they are not getting logo in email – Rakesh Apr 10 '18 at 13:00
  • Gmail reprocesses all their emails and they don't support it. Even when you have saved it on your server, Gmail still rewrites the images and put it on their own server. – Chibueze Opata Apr 10 '18 at 20:38
0

You need to save the image as a file and point to it's source, or alternatively, read about 'Data URIs'.

Look at This answer.

Idan Levi
  • 388
  • 3
  • 18
  • That would be a good answer if you can elaborate a bit more (a code sample would be good). – trailmax Apr 05 '18 at 09:59
  • as per requirement they told it is not going to save anywhere so I need to get answer that this image How I can append now without saving anywhere. There are 1000 Clients and their 1000 customers of each client so how many i will save ? – Rakesh Apr 05 '18 at 10:06