Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C#
  4. Convert HTML String To Image In C#

Convert HTML String To Image In C#

Date- Jul 02,2022 Updated Feb 2026 11491 Free Download Pay & Download
HTMLtoImage C#

Hello, guys, and welcome to Code2Night! In this article, we will see how to Convert HTML String To Image In C#

Have you ever found yourself working on an ASP.NET web project and encountered the need to convert your HTML output into an image? Perhaps you wanted to save the rendered HTML as an image file on your system. If so, you're in the right place! In this introductory guide, we will explore how to convert an HTML string to an image using C# in ASP.NET.

Converting HTML to an image can be quite beneficial in a variety of situations. It allows you to capture the visual representation of your web page or HTML content and save it as an image file, which is useful for making reports, thumbnails, and sharing visual representations of your HTML output.

In the context of ASP.NET, C# provides a powerful set of tools and libraries that enable us to accomplish this task efficiently. By leveraging the capabilities of these tools, we can transform HTML code into image files effortlessly.

Throughout this tutorial, we will walk you through the process step by step. We will start by exploring the different approaches available for converting HTML to an image in C# and then delve into a specific method using ASP.NET. By the end of this tutorial, you'll have a solid understanding of how to convert an HTML string to an image in your ASP.NET web project using C#.

So, let's dive right in and unlock the secrets of converting HTML to an image in C# ASP.NET!


So, in order to export your HTML to an image, we can utilize the WebBrowser class, which is available in ASP.NET MVC. To begin, let's create the HTML that we wish to export as an image. You can copy the method from the code provided below. Additionally, please make sure to include all necessary dependencies and libraries for this process.

using System;  
using System.Drawing;  
using System.Windows.Forms;  
using System.Threading;   
 public void CreateHtmlToImage()
        {
            var source = @"<!DOCTYPE html>
                                            <html>
                                            <head><meta http-equiv='X-UA-Compatible' content='IE=Edge' />
                                            <style>
                                            table {
                                              font-family: arial, sans-serif;
                                              border-collapse: collapse;
                                              width: 100%;
                                            }

                                            td, th {
                                              border: 1px solid #dddddd;
                                              text-align: left;
                                              padding: 8px;
                                            }

                                            tr:nth-child(even) {
                                              background-color: #dddddd;
                                            }
                                            </style>
                                            </head>
                                            <body style='width:1000px'>

                                            <h2>HTML Table</h2>

                                                <table style='width:1000px'>
                                                  <tr>
                                                    <th>Company</th>
                                                    <th>Contact</th>
                                                  </tr>
                                                  <tr>
                                                    <td>Alfreds Futterkiste</td>
                                                    <td>Maria Anders</td>
                                                  </tr>
                                                  <tr>
                                                    <td>Centro comercial Moctezuma</td>
                                                    <td>Francisco Chang</td>
                                                  </tr>
                                                  <tr>
                                                    <td>Ernst Handel</td>
                                                    <td>Roland Mendel</td>
                                                  </tr>
                                                  <tr>
                                                    <td>Island Trading</td>
                                                    <td>Helen Bennett</td>
                                                  </tr>
                                                  <tr>
                                                    <td>Laughing Bacchus Winecellars</td>
                                                    <td>Yoshi Tannamuri</td>
                                                  </tr>
                                                  <tr>
                                                    <td>Magazzini Alimentari Riuniti</td>
                                                    <td>Giovanni Rovelli</td>
                                                  </tr>
                                                </table>

                                            </body>
                                            </html>";
            var filename = @"F://Image.jpg";
            var th = new Thread(() =>
            {
                var webBrowser = new WebBrowser();
                webBrowser.ScrollBarsEnabled = false;
                webBrowser.IsWebBrowserContextMenuEnabled = true;
                webBrowser.AllowNavigation = true;

                webBrowser.DocumentCompleted += webBrowserDocumentCompleted;
                webBrowser.DocumentText = source;
                webBrowser.Name = filename;
                webBrowser.Width = 1000;
                Application.Run();
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();

            Task.Run(() => th);
            Task.WaitAll();
        }

So, after you use this on your controller, you have to add this Completed event, which will be fired once your HTML output is ready to be saved as an image. It will save the bitmap image to the location specified by the filename parameter. Now, execute the code, and you will see that your HTML is saved as an image in the specified location. This functionality ensures that you can easily convert your HTML content into visual representations, allowing for convenient sharing or further manipulation if needed. By incorporating this feature into your controller, you enhance the versatility and usability of your application. Feel free to explore the various possibilities and creative applications enabled by this capability.

static void webBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var webBrowser = (WebBrowser)sender;
            int scrollWidth = webBrowser.Document.Body.ScrollRectangle.Width;
            int scrollHeight = webBrowser.Document.Body.ScrollRectangle.Height;
            using (Bitmap bitmap = new Bitmap(scrollWidth, scrollHeight))
            {
                webBrowser.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height));
                bitmap.Save(webBrowser.Name, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }

Convert HTML

This is the image output that we got in our case. So this is how you can convert html string to the image in c# asp.net.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Linkedin Sign In using LinkedinLogin Nuget package in Asp-Net MVC
Apr 14, 2023
How to export view as pdf in Asp.Net Core
Jul 05, 2022
Linkedin Sign In using LinkedinLogin Nuget package in Asp-Net MVC
Jul 05, 2022
Excel Export in Asp.Net MVC using XlWorkbook
Jun 11, 2022
Previous in C#
The report definition is not valid or is not supported by this ve…
Next in C#
Compress image using ImageCompress NuGet package
Buy me a pizza

Comments

On this page

🎯

Interview Prep

Ace your C# interview with curated Q&As for all levels.

View C# Interview Q&As

More in C#

  • Zoom C# Wrapper Integration 12905 views
  • The report definition is not valid or is not supported by th… 10840 views
  • Replacing Accent Characters with Alphabet Characters in CSha… 9812 views
  • Get IP address using c# 8666 views
  • How to Convert DataTable to List 7656 views
View all C# posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor