Convert HTML to BMP in C#
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 for .NET, 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 for .NET 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:
- Load an HTML file using the HTMLDocument class ( bmp.html).
- Create a new
ImageSaveOptions object with
ImageFormat.Bmp
. By default, the Format property is PNG. - 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.
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "bmp.html");
3
4// Prepare a path to save the converted file
5string savePath = Path.Combine(OutputDir, "bmp-output.bmp");
6
7// Initialize an HTML document from the file
8using var document = new HTMLDocument(documentPath);
9
10// Create an instance of the ImageSaveOptions class
11var options = new ImageSaveOptions(ImageFormat.Bmp);
12
13// Convert HTML to BMP
14Converter.ConvertHTML(document, options, savePath);
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.
Property | Description |
---|---|
Compression | Sets Tagged Image File Format (TIFF) Compression. By default, this property is LZW. |
CSS | Gets a CssOptions object which is used for configuration of CSS properties processing. |
Format | Sets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG. |
BackgroundColor | This property sets the color that will fill the background. By default, this property is Transparent. |
PageSetup | This property gets a page setup object and uses it for configuration output page-set. |
HorizontalResolution | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
VerticalResolution | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
UseAntialiasing | This property sets the image rendering quality. Antialiasing is enabled by default. |
Text | Gets 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:
- Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class ( bmp.html).
- 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.
- 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:
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "bmp.html");
3
4// Prepare a path for converted file saving
5string savePath = Path.Combine(OutputDir, "bmp-output-options.bmp");
6
7// Initialize an HTML Document from the html file
8using var document = new HTMLDocument(documentPath);
9
10// Initialize ImageSaveOptions
11var options = new ImageSaveOptions(ImageFormat.Bmp)
12{
13 UseAntialiasing = false,
14 HorizontalResolution = 350,
15 VerticalResolution = 350,
16 BackgroundColor = System.Drawing.Color.Beige
17};
18
19// Convert HTML to BMP
20Converter.ConvertHTML(document, options, savePath);
In the above example, we apply:
BackgroundColor
property that sets the color that will fill the background. The default BackgroundColor is Transparent.HorizontalResolution
andVerticalResolution
properties that set horizontal/vertical resolution for output images in pixels per inch. By default, these properties are 300 dpi.UseAntialiasing
property that sets the rendering quality for this image. This example usesUseAntialiasing = false
for simpler, performance-oriented rendering that does not require antialiasing.
Use UseAntialiasing = true
when you want to improve the visual quality of rendered shapes, text, and images in your application, especially when clarity and smooth edges are essential. Enabling antialiasing smooths out jagged edges by blending the colors of pixels around the edges, resulting in a softer, more refined look.
While UseAntialiasing = true
provides better visual quality, it can also increase processing time. For applications where rendering speed is a priority, it may be optimal to set UseAntialiasing = false
.
The figure shows the bmp-output-options.bmp file.
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!