-3

How to convert a dynamic link which is a html web page into an Image format. Remember the link is dynamic which contains html content in string format. I have tried a lot of ways like reading the html content using converting to base64 first then visa versa.

var htmlToImageConv = new HtmlToImageConverter();
 byte[] jpegBytes = htmlToImageConv.GenerateImage(html, ImageFormat.Jpeg); System.Drawing.Image image; using (System.IO.MemoryStream ms = new System.IO.MemoryStream(strOg))
 { 
image = System.Drawing.Image.FromStream(ms); string path = Server.MapPath("~/images/"); 
} 

I have tried this code in c# for converting html webpage to image.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ankita
  • 189
  • 1
  • 4
  • 12
  • 2
    You should try adding the things you have tried to your question, this will make the question more valid, for other to repond, otherwise there is a chance its can get closed. – Mark Redman Aug 08 '19 at 14:10

3 Answers3

2

You can use a headless browser to render the html and then take a snapshot.

Have a look at PuppeteerSHarp: https://github.com/kblok/puppeteer-sharp

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
2

You could use Selenium to render the page and save a screenshot as a png image.

Add the following packages to your project:

  • Selenium.WebDriver
  • Selenium.Chrome.WebDriver

Use the following code to save a screenshot:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var driver = new ChromeDriver();
            driver.Navigate().GoToUrl("http://google.com");
            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();

            ss.SaveAsFile("screenshot.png");
        }
    }
}
Alberto
  • 15,626
  • 9
  • 43
  • 56
  • This works but I am still having an issue. The above code is taking the screenshot of the whole page. What happens when a user clicks on the link the html web page opens in iframe. That iframe will have extra background with it which I do not want to include in the screenshot. Is it possible if I can only take the screenshot of img inside iframe. – Ankita Aug 13 '19 at 07:50
  • Or maybe if its possible to take the screenshot without opening the url in the browser. – Ankita Aug 13 '19 at 07:52
0

That what you need is a conversation from a html containing string to an image, which is already discussed in the answers of this Question.

halfer
  • 19,824
  • 17
  • 99
  • 186
Oliver
  • 93
  • 1
  • 7