0

I am trying to convert a HTML string to an image using bitmap. I convert it, but it is very pixelated, with low quality. The code I'm using is the following.

string pdfDest = HttpContext.Current.Server.MapPath("~/serverFiles/caratula.pdf");
var tabla = "<!DOCTYPE html><body><table id =\"caratula\" class=\"tabla-caja mb-4\" summary=\"Justificante\" style=\"border: 1px solid #aeccd3; float: right; color: #394146; font-family: 'Roboto'; font-size: 0.9em; padding: -2px;\"> <tbody> <tr><td style=\"padding: 5px 10px; text-align: right; border-right: 1px solid #aeccd3;border-color: #aeccd3; background-color: #FFFFFF\" scope=\"col\">21 Zabalgunea Bulegoa<br> <span id=\"tipoDocEU\">SARRERA</span></td> <td class=\"text-start light\" style=\"padding: 5px 10px; background-color: #FFFFFF\" scope=\"col\">Oficina Ensanche 21<br> <span id=\"tipoDocES\">ENTRADA</span></td> </tr></tbody> <tfoot> <tr><td scope=\"col\" colspan=\"2\" style=\"border-top: 1px solid #aeccd3; text-align: center; padding: 5px 10px; margin-top: -2px; background-color: #FFFFFF\"><span id=\"cabecera\" style=\"padding-right: 0.5em; padding-left: 0.5em; text-transform: uppercase;\">02/05/2022 12:41 - 2022RPE00000168</span></td></tr></tfoot> </table></body></html>";


using (var ms = new MemoryStream(10 * 1024))
using (var reader = new PdfReader(file))
using (var stamper = new PdfStamper(reader, ms))
{
    int numberOfPages = reader.NumberOfPages;
    string caratula = HttpContext.Current.Server.MapPath("~/serverFiles/caratula.png");
    var ps = reader.GetPageSizeWithRotation(1); /*dc.PdfDocument.PageSize is not always correct*/
    var x = ps.Right - 270;
    var y = ps.Top - 105;
    System.Drawing.Bitmap m_Bitmap = new System.Drawing.Bitmap(355, 110);
    System.Drawing.PointF point = new System.Drawing.PointF(10, 10);
    System.Drawing.SizeF maxSize = new System.Drawing.SizeF(355, 110);
    TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderGdiPlus(System.Drawing.Graphics.FromImage(m_Bitmap),
        tabla.Replace("\"", "\'"),
        point, maxSize);
    m_Bitmap.SetResolution(355, 110);
    m_Bitmap.Save(caratula, System.Drawing.Imaging.ImageFormat.Png);
}

What is wrong or how can I solve my problem? Thanks

Arialdo Martini
  • 4,427
  • 3
  • 31
  • 42
agoya
  • 1
  • 3
  • Does this answer your question? [Convert HTML string to image](https://stackoverflow.com/questions/17832304/convert-html-string-to-image) – SH7 May 03 '22 at 11:29
  • You mean using HtmlRenderer.HtmlRender.Render instead of TheArtOfDev.HtmlRenderer.WinForms.HtmlRender.RenderGdiPlus? – agoya May 03 '22 at 11:35

0 Answers0