DsImaging library along with DsHtml library lets you easily render HTML content to Images. When you browse through the content on a website, you may want to capture images to incorporate them into a professional presentation. Sometimes, one may also want to take the snapshot of online pricing details. With a utility library like DsHtml,the user can conveniently render HTML content to high resolution images. With DsHtml, you can convert webpages, HTML strings or even URIs to different image formats (JPEG, PNG, BMP, TIFF, GIF and WebP).
DsHtml is based on the industry standard Chrome web browser engine working in headless mode, offering advantage of rendering HTML to image on any platform - Windows, Linux and macOS. It doesn’t matter whether your .NET application is built for x64, x86 or AnyCPU platform target. The browser is always working in a separate process.
The DS.Documents.Html package contains the following namespaces:
C# |
Copy Code
|
---|---|
using GrapeCity.Documents.Html; using GrapeCity.Documents.Pdf; using GrapeCity.Documents.Drawing; |
C# |
Copy Code
|
---|---|
using GrapeCity.Documents.Html; using System.Drawing; var browserPath = BrowserFetcher.GetSystemChromePath(); using var browser = new GcHtmlBrowser(browserPath); var uri = new Uri("Sample2.html", UriKind.Relative); using var pg = browser.NewPage(uri, new PageOptions { WindowSize = new Size(700, 300), DefaultBackgroundColor = Color.AliceBlue }); pg.SaveAsPng("Sample2.png"); |
The resulting image is shown below:
Note:
C# |
Copy Code
|
---|---|
using GrapeCity.Documents.Imaging; using GrapeCity.Documents.Html; using System.Drawing; var browserPath = BrowserFetcher.GetSystemEdgePath(); using var browser = new GcHtmlBrowser(browserPath); string html = "<p style=\"color: green; text-shadow: 3px 3px 3px gray;\">JavaScript can change HTML content.</p>"; using var pg = browser.NewPage(html, new PageOptions { DefaultBackgroundColor = Color.LemonChiffon }); using var bitmap = new GcBitmap(); pg.RenderAndCrop(bitmap, new PngOptions { Scale = 3 }, Color.LemonChiffon, 100, 50, 20, 100); bitmap.SaveAsTiff("Sample3.tiff"); |
The resulting image is shown below:
The user can also render HTML content to image using the DrawHtml method of GcBitmapGraphicsExt class. The DrawHtml method allows to convert an HTML text or page into an image. It also allows to insert HTML fragments in images along with other content. Moreover, the DrawHtml method has two overloads. The GcBitmapGraphics.DrawHtml (string html, float x, float y, HtmlToImageFormat format, out SizeF size) can be used to draw an HTML text on GcBitmapGraphics at a specified position, while the GcBitmapGraphics.DrawHtml (Uri htmlUri, float x, float y, HtmlToImageFormat format, out SizeF size) can be used to draw an HTML page specified by an URI on GcBitmapGraphics at a specified position.
To render HTML string or page on GcBitmapGraphics at specified position, follow the steps below:
C# |
Copy Code
|
---|---|
//Create an instance of GcBitmap class var bmp = new GcBitmap(1000, 1000, true, 96, 96); //Configure image settings HtmlToImageFormat htmlToImage = new HtmlToImageFormat(true) { WindowSize = new Size(500, 500) }; htmlToImage.DefaultBackgroundColor = Color.White; //Draw HTML Page on GcBitmapGraphics using (var g = bmp.CreateGraphics(Color.LightBlue)) { SizeF addSize = new SizeF(); // Create an instance of GcHtmlBrowser that is used to render HTML: var browserPath = BrowserFetcher.GetSystemChromePath(); var browser = new GcHtmlBrowser(browserPath); g.DrawHtml(browser, new Uri("https://www.apple.com/in/"), 10, 10, htmlToImage, out addSize); bmp.SaveAsJpeg("DrawHtml.jpeg"); } |
The resulting image is shown below:
For more information about rendering HTML to Image using DsImaging, see DsImaging demo.
If your application uses obsolete GcHtmlRenderer class to convert the HTML pages or content to an image format, you can use following steps to quickly update to the new GcHtmlBrowser class which does not depend on a custom build of Chromium and does not require GPL or LGPL licenses.
C# |
Copy Code
|
---|---|
GcHtmlRenderer.SetGcImagingLicenseKey(key); -> GcHtmlBrowser.SetGcImagingLicenseKey(key); GcHtmlRenderer.SetGcPdfLicenseKey(key); -> GcHtmlBrowser.SetGcPdfLicenseKey(key); |
C# |
Copy Code
|
---|---|
var path = BrowserFetcher.GetSystemChromePath();
|
C# |
Copy Code
|
---|---|
var tp = Path.GetTempPath(); var bf = new BrowserFetcher() { DestinationFolder = Path.Combine(tp, ".gc-chromium") }; var path = bf.GetDownloadedPath(); |
C# |
Copy Code
|
---|---|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return new GcHtmlBrowser(path, new LaunchOptions { RunWithNoSandbox = true }); else return new GcHtmlBrowser(path) |
C# |
Copy Code
|
---|---|
g.DrawHtml(html, ...); -> g.DrawHtml(browser, html, ...); |
C# |
Copy Code
|
---|---|
using var re = new GcHtmlRenderer(uri); ... re.RenderToJpeg(file, new JpegSettings() {...}); re.RenderToPng(file, new PngSettings() {...}); |
C# |
Copy Code
|
---|---|
// Create an HtmlPage from the URI // (DefaultBackgroundColor and WindowSize options from Pdf/Jpeg/PngSettings // have moved to PageOptions, while some other options are now in LaunchOptions): using var htmlPage = browser.NewPage(uri, new PageOptions() { WindowSize = pixelSize;… }); ... htmlPage.SaveAsJpeg(file, new JpegOptions() {...}); htmlPage.SaveAsPng(file, new PngOptions() {...}); |