Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to PNG in C#, use
Converter.ConvertHTML() with an HTML source,
ImageSaveOptions, and an output path. ImageSaveOptions lets you control the image format, page setup, background color, resolution, CSS media type, and rendering quality.
This guide shows the core HTML to PNG workflows for C# applications: a one-line conversion, a file-based conversion, custom image rendering options, and output stream providers. Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.
PNG is a lossless image format widely used for screenshots, documentation assets, UI previews, and web publishing. Use HTML to PNG conversion when your application needs to render a web page, generated HTML, or template output into a raster image with predictable layout and visual quality.
For simple input, the static
Converter class provides a short path from HTML content to a PNG image. The example below passes HTML code, a base URI, default ImageSaveOptions, and the output file path to ConvertHTML().
1// Convert HTML to PNG using C#
2
3// Invoke the ConvertHTML() method to convert HTML to PNG
4Converter.ConvertHTML(@"<h1>Convert HTML to PNG!</h1>", ".", new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));Use this pattern when your HTML is already available as a string and default PNG rendering settings are enough. For file input, page setup, resolution, or custom output streams, use the workflows below.
A typical file-based conversion uses an
HTMLDocument instance, an ImageSaveOptions object, and Converter.ConvertHTML(). By default, ImageSaveOptions uses PNG as the output image format.
The workflow is:
HTMLDocument constructor.ImageSaveOptions instance.Converter.ConvertHTML(document, options, savePath) to render the HTML document as a PNG image.The following C# code converts an HTML document to PNG.
1// Convert HTML to PNG in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "nature.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "nature-output.png");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create an instance of the ImageSaveOptions class
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
14
15// Convert HTML to PNG
16Converter.ConvertHTML(document, options, savePath);Use PNG output when the rendered HTML page must keep sharp text, line art, UI details, or a transparent background. PNG is a strong choice for documentation screenshots, diagrams, code-heavy pages, and lossless previews. If smaller photographic thumbnails are more important than crisp edges, HTML to JPG may be a better fit. If the output must remain a document, use HTML to PDF.
PNG is the default ImageSaveOptions.Format value, so a plain ImageSaveOptions instance already targets PNG output. You can still adjust page setup, background color, resolution, antialiasing, and stream-based output.
Use
ImageSaveOptions when the output image needs custom rendering settings. The same options class is used for PNG, JPG, BMP, GIF, and TIFF output; for PNG, the default Format value is ImageFormat.Png.
| Option | Use it to control |
|---|---|
Format | Output image format, such as PNG, JPG, BMP, GIF, or TIFF. The default value is ImageFormat.Png. |
PageSetup | Output page size and margins used during rendering. |
BackgroundColor | Background fill color. The default value is transparent. |
HorizontalResolution | Horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
VerticalResolution | Vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
UseAntialiasing | Rendering quality for shapes, text, and images. Antialiasing is enabled by default. |
CSS | CSS media type and related CSS processing options. |
Text | Text rendering options for image output. |
The following example creates a PNG file with custom image save options.
1// Convert HTML to PNG in C# with custom settings
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "nature.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "nature-output-options.png");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions()
14{
15 UseAntialiasing = false,
16 HorizontalResolution = 100,
17 VerticalResolution = 100,
18 BackgroundColor = System.Drawing.Color.Beige
19};
20
21// Convert HTML to PNG
22Converter.ConvertHTML(document, options, savePath);In this example, ImageSaveOptions is passed to Converter.ConvertHTML() together with the loaded HTML document and output path. The sample configures background color, horizontal and vertical resolution, and antialiasing behavior.
Use UseAntialiasing = true when visual smoothness is more important than rendering speed. Use UseAntialiasing = false for simpler, performance-oriented rendering where sharper edges or faster processing are acceptable.
Image formats can produce multiple output files, especially when rendered content spans more than one page. If your application needs to save generated PNG files to custom storage, such as memory, a database, cloud storage, or an archive, implement ICreateStreamProvider.
The ICreateStreamProvider interface lets your code create a stream at the beginning of document or page rendering and release it after rendering is complete.
The example below implements a custom MemoryStreamProvider.
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}The following C# code uses MemoryStreamProvider during HTML to PNG conversion and then saves the generated stream content to a file.
1// Convert HTML to PNG in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Initialize an HTML document
7using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to PNG File Format!</h1>", ".");
8
9// Convert HTML to JPG using the MemoryStreamProvider
10Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Png), streamProvider);
11
12// Get access to the memory stream that contains the result data
13MemoryStream memory = streamProvider.Streams.First();
14memory.Seek(0, SeekOrigin.Begin);
15
16// Flush the result data to the output file
17using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.png")))
18{
19 memory.CopyTo(fs);
20}PdfSaveOptions.DocSaveOptions.Use the online HTML to PNG Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML to PNG rendering must run inside a C# application, service, reporting tool, or image-generation workflow.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.