fpanaccia / Wkhtmltopdf.NetCore.Example-deprecated

Implementation of Wkhtmltopdf.NetCore
81 stars 33 forks source link

Images #23

Open leselemannathoko opened 2 years ago

leselemannathoko commented 2 years ago

Good day everyone,

Can someone help me with displaying Image in PDF...

Jefferycheng commented 2 years ago

You could read the image from your directory and parse the image to Base64.

The example code :

C#

private string GenerateImgateHTML()
{
     return $@"<img src=""data:image/png;base64,{ImgToBase64String(<<--image path-->>)}"" />";
}

private string ImgToBase64String(string imageFileName)
{
    try
    {
         var bmp = new Bitmap(imageFileName);
         var ms = new MemoryStream();
         bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         var arr = new byte[ms.Length];
         ms.Position = 0;
         ms.Read(arr, 0, (int)ms.Length);
         ms.Close();
         return Convert.ToBase64String(arr);
      }
     catch (Exception ex)
     {
        return null;
     }
 }