Convert HTML to BMP | C# Examples

BMP files represent Bitmap Image files that are used to store high-quality bitmap digital images. The BMP file format can store data as two-dimensional digital images in both monochrome as well as color format with various color depths. It can save color data for each pixel in an image without any compression. With Aspose.HTML, you can convert HTML to BMP format programmatically with full control over a wide range of conversion parameters.

In this article, you find information on how to convert HTML to BMP by using ConvertHTML() methods of the Converter class, and how to apply ImageSaveOptions and ICreateStreamProvider parameters.

Online HTML Converter

You can check the Aspose.HTML API functionality and convert HTML in real-time. Please load HTML from the local file system, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.

                
            

If you want to convert HTML to BMP programmatically, please see the following C# code examples.

Convert HTML to BMP

Converting a file to another format using the ConvertHTML() method is a sequence of operations among which document loading and saving:

  1. Load an HTML file using the HTMLDocument class ( bmp.html).
  2. Create a new ImageSaveOptions object with BMP ImageFormat. By default, the Format property is PNG.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a BMP image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method to convert HTML to BMP.

Please take a look over the following C# code snippet which shows the process of converting HTML to BMP using Aspose.HTML for .NET.

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Converters;
 4using Aspose.Html.Rendering.Image;
 5using Aspose.Html.Saving;
 6...
 7    // Prepare a path to a source HTML file
 8    string documentPath = Path.Combine(DataDir, "bmp.html");
 9
10    // Prepare a path for converted file saving 
11    string savePath = Path.Combine(OutputDir, "bmp-output.bmp");
12
13    // Initialize an HTML document from the file
14    using var document = new HTMLDocument(documentPath);
15
16    // Initialize ImageSaveOptions 
17    var options = new ImageSaveOptions(ImageFormat.Bmp);
18
19    // Convert HTML to BMP
20    Converter.ConvertHTML(document, options, savePath);

You can download the complete examples and data files from GitHub.

Save Options

Aspose.HTML allows converting HTML to BMP using default or custom save options. ImageSaveOptions usage enables you to customize the rendering process; you can specify the image format, page size, margins, compression level, CSS media-type, etc.

PropertyDescription
CompressionSets Tagged Image File Format (TIFF) Compression. By default, this property is LZW.
CSSGets a CssOptions object which is used for configuration of CSS properties processing.
FormatSets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG.
BackgroundColorThis property sets the color that will fill the background. By default, this property is Transparent.
PageSetupThis property gets a page setup object and uses it for configuration output page-set.
HorizontalResolutionSets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolutionSets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
SmoothingModeThis property sets the rendering quality for this image. Available values are Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias.
TextGets a TextOptions object which is used for configuration of text rendering.

To learn more about the ImageSaveOptions class, please read the Fine-Tuning Converters article.

Convert HTML to BMP using ImageSaveOptions

To convert HTML to BMP with ImageSaveOptions specifying, you should follow a few steps:

  1. Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class ( bmp.html).
  2. Create a new ImageSaveOptions object with BMP ImageFormat and specify save options. By default, the Format property is PNG. The ImageSaveOptions() constructor initializes an instance of the ImageSaveOptions class that is passed to ConvertHTML() method.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a BMP image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method to convert HTML to BMP.

The following C# code snippet shows how to convert HTML to BMP using custom save options:

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Converters;
 4using Aspose.Html.Rendering.Image;
 5using Aspose.Html.Saving;
 6using System.Drawing;
 7using System.Drawing.Drawing2D;
 8...
 9    // Prepare a path to a source HTML file
10    string documentPath = Path.Combine(DataDir, "bmp.html");
11
12    // Prepare a path for converted file saving
13    string savePath = Path.Combine(OutputDir, "bmp-output-options.bmp");
14
15    // Initialize an HTML Document from the html file
16    using var document = new HTMLDocument(documentPath);
17
18    // Initialize ImageSaveOptions 
19    var options = new ImageSaveOptions(ImageFormat.Bmp)
20    {
21        SmoothingMode = SmoothingMode.Default,
22        HorizontalResolution = 350,
23        VerticalResolution = 350,
24        BackgroundColor = System.Drawing.Color.Beige
25    };
26
27    // Convert HTML to BMP
28    Converter.ConvertHTML(document, options, savePath);    

In the above example, we apply:

The ImageSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to Image formats. Among these properties, SmoothingMode that enables you to set the rendering quality for the image. Available values are Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias. You can select any value, considering the advantages and disadvantages of each one.

The figure shows the bmp-output-options.bmp file.

Text “BMP image”

Aspose.HTML offers a free online HTML to BMP Converter that converts HTML to BMP image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “Banner HTML to BMP Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.