1

I'm developing a C# forms application, and I need to render HTML during the paint event of a PictureBox control.

Showing just the relevant part, this is the way I would like to do this.

private void CustomPictureBoxPaint(object sender, PaintEventArgs e) {
    Graphics g = e.Graphics;
    //render html as on g
}

I found a NuGet library which is able to do it this way. HtmlRenderer.Core has a HtmlRenderer class.

private void CustomPictureBoxPaint(object sender, PaintEventArgs e) {
    Graphics g = e.Graphics;
    Image image = HtmlRender.RenderToImage(htmlText,new Size(150,150) //htmlText has the HTML as string
    g.DrawImage(image,point);
}

But there is one big restriction for it: the background can't be transparent. It is something I need, so my question is the following:

Is there a library which is capable of rendering html with transparent background on a PictureBox, or any other way to achieve my goal?
Alternatively rendering markdown instead of html is also a good solution for me, if there is a way to do it.

Laci Mazug
  • 11
  • 2
  • Transparency doesn't work on winform between 2 controls. Your best bet is to replace the transparency color by the parent backgroundcolor – Franck Nov 25 '20 at 19:00
  • @Franck Yes, it does. For example, take this custom control: [Translucent circular Control with text](https://stackoverflow.com/a/51435842/7444103), drop it on a Form and make it overlap two or more other controls. You'll see :) That *sample* doesn't handle scrolling controls, but that can be done, too. Anyway, this is not really the point. What parts of a Web page should be transparent? The *background* of the Document? What about all the DIV elements inside it that probably cover the entire surface? You could draw the Document content to a Bitmap and call `MakeTransparent`, but... – Jimi Nov 25 '20 at 20:31
  • You could take this: [WebBrowser Html Document to Image](https://stackoverflow.com/a/60741246/7444103) and render to a transparent DC. Not sure it will be pretty. – Jimi Nov 25 '20 at 20:31
  • @Jimi Thanks for the suggestion. I was able to use the code you suggested to draw the image, but I couldn't make it transparent. Could you specify how you meant it? – Laci Mazug Nov 26 '20 at 12:32

0 Answers0