0

System.Drawing is not supported in Azure Functions, therefore Image class cannot be used to convert byte array into Image. Then how do we convert byte arrays into Image in Azure Functions that uses C#Script where System.Drawing is not recognized as a valid namespace

The images are stored in a database in BYTE ARRAY format. Later when the byte arrays are needed to be converted to image, to embed them in the emails, the code displays error since Image namespace is not recognized in Azure functions. System.Drawing dll is not supported in Azure Function. Is there an alternative to the below code to convert byte array to Images:

MemoryStream imageMemoryStream = new MemoryStream(imageFromDatabase.Data);
Image imageFromStream = Image.FromStream(imageMemoryStream);
var inlineImage = new LinkedResource(imageFromStream, imageFromDatabase.ContentType)
{
    ContentId = Guid.NewGuid().ToString()
};
att.Value = string.Format("cid:{0}", inlineImage.ContentId);
linkedResources.Add(inlineImage);

Expected results: Byte arrays are converted to Images

  • So the actual question is `where is System.Drawing` in Azure Functions? – Panagiotis Kanavos Sep 30 '19 at 09:28
  • In any case this code doesn't convert byte arrays to images. Those byte arrays already contain the images. That code loads the bytes of the images into a System.Drawing.Image class. If you can't use the `System.Drawing` namespace you don't even have that class. What are you *really* trying to do? How is `linkedResources` used and why load the images into `Image`? – Panagiotis Kanavos Sep 30 '19 at 09:30
  • `to embed them in the emails` in that case you need to *attach* those byte arrays to your emails, not load them into Image objects. – Panagiotis Kanavos Sep 30 '19 at 09:31
  • Possible duplicate of [Can System.Drawing be used in an Azure web site?](https://stackoverflow.com/questions/40924122/can-system-drawing-be-used-in-an-azure-web-site) – Mohit Verma Oct 01 '19 at 09:07

1 Answers1

0

To embed image in email, you do not need to use System.Drawing. You juse need to put the image content to your email message. Here is my sample:

public static class Function1
{
    [FunctionName("Function1")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]HttpRequest req, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        // To simplify the test, I diredtly get the image bytes from base64encoded string. 
        string base64String = "/9j/4gIcSUNDX1BST0ZJT********NjN8uMCgsR4lzsgB7Q6gAggunL9Lgw88jkGAPOlL3kk2gggud11//Z";
        byte[] jpg = Convert.FromBase64String(base64String);

        using (MemoryStream stream = new MemoryStream(jpg))
        {
            LinkedResource res = new LinkedResource(stream, MediaTypeNames.Image.Jpeg);
            res.ContentId = Guid.NewGuid().ToString();
            string htmlBody = $"<html><body><h1>Picture</h1><br><img src=\"cid:{res.ContentId}\" /></body></html>";
            AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
            alternateView.LinkedResources.Add(res);

            MailMessage message = new MailMessage(new MailAddress("jack@hanxia.onmicrosoft.com"), new MailAddress("jialinjun@gmail.com"));
            message.Subject = "Send email with imgae";
            message.AlternateViews.Add(alternateView);

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.office365.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential("jack@hanxia.onmicrosoft.com", "***the_password***");
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(message);
            return (ActionResult)new OkObjectResult($"{htmlBody}");
        }
    }
}

After calling the function, I can get a new email in jialinjun@gmail.com: enter image description here

Jack Jia
  • 5,268
  • 1
  • 12
  • 14