Exception when converting to image from Base-64 string

I am trying to send a Highcharts chart through an image on an ASP.NET button click. I am trying to do this:

Convert the diagram to base64 image, the code is as follows:

  var chart = $('#main-content').highcharts();
    EXPORT_WIDTH = 1000;
    var render_width = EXPORT_WIDTH;
    var render_height = render_width * chart.chartHeight / chart.chartWidth;

    var svg = chart.getSVG({
        exporting: {
            sourceWidth: chart.chartWidth,
            sourceHeight: chart.chartHeight
        }
    });
    var contentToSend = 'data:image/svg+xml;base64,' + window.btoa(svg);
    var hdnField = document.getElementById("MainContent_ChartImage");
    hdnField.value = contentToSend;

      

The next step takes a base64 image value, converts it to an image by attaching it to the mail, code:

 string textImage = ChartImage.Value;

 var imageData = Convert.FromBase64String(HttpUtility.UrlDecode(data));
 System.Net.Mail.LinkedResource res;
 AlternateView htmlView;
 using (MemoryStream ms = new MemoryStream(imageData, true))
 {
      ms.Position = 0;
      ms.Write(imageData, 0, imageData.Length);
      ms.Seek(0, SeekOrigin.Begin);
      res = new System.Net.Mail.LinkedResource(ms);
      htmlView = AlternateView.CreateAlternateViewFromString("<html><body><img src='cid:imageReport' width='100%' ></body></html>", null, "text/html");
      res.ContentId = "imageReport";
      htmlView.LinkedResources.Add(res);
      MailMessage mailMsg = new MailMessage();
      SmtpClient client = new SmtpClient();

      // ...

      mailMsg.IsBodyHtml = true;
      mailMsg.AlternateViews.Add(htmlView);
      client.Send(mailMsg);
 }

      

but the method Convert.FromBase64String

throws an exception

{"The input is not a valid Base-64 string because it contains a non-base 64 character, more than two indents, or an invalid uppercase character." }

However, when I remove the 'data: image / svg + xml; base64, 'and then converting it, it does not throw an exception, but the image will not be displayed. What should I do?

thank

+3


source to share


2 answers


After a lot of research I found a solution, the main problem was that not all client data URIs are for email support: What is data URI support, for example, in main mail client?

I tried to open mail from Outlook 2016 but it is not supported, when I opened from hotmail.com it worked.



code:

MailMessage mailMsg = new MailMessage();
SmtpClient client = new SmtpClient();
var imageData = Convert.FromBase64String(data);
var contentId = Guid.NewGuid().ToString();
var linkedResource = new LinkedResource(new MemoryStream(imageData), "image/svg+xml");
linkedResource.ContentId = contentId;
linkedResource.TransferEncoding = TransferEncoding.Base64;
var body = string.Format("<img src=\"cid:{0}\" />", contentId);
var htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
 htmlView.LinkedResources.Add(linkedResource);
 mailMsg.AlternateViews.Add(htmlView);

      

0


source


Get rid of the initial part of the line: "data: image / svg + xml; base64", this part is not base64, just the rest. You also don't need to use HttpUtility.UrlDecode.

You must specify TransferEncoding as Base64:



res.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;

      

With all that being said , however, there are some strong caveats about using SVG in email . Thus, you can consider another format such as JPG or PNG. If you take this route, you will need to use a library to convert formats.

+1


source







All Articles