Convert HTML to GIF | C#

Using Converter.ConvertHTML methods is the most common way to convert HTML code into various formats. With Aspose.HTML, you can convert HTML to GIF format programmatically with full control over a wide range of conversion parameters.

GIF is a popular image format that supports animated images and frequently used in web publishing. HTML to GIF conversion allows you to save an HTML document as a GIF image. In this article, you find information on how to convert HTML to GIF 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.

Code example in C# to convert document formats
Input file
Upload a file you want to convert
Output format
Select the target format

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

Convert HTML to GIF

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 (spring.html).
  2. Create a new ImageSaveOptions object with GIF ImageFormat. By default, the Format property is PNG.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a GIF image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method for HTML to GIF conversion.

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

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
...
    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "spring.html");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "spring-output.gif");
    
    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions(ImageFormat.Gif);
    
    // Convert HTML to GIF
    Converter.ConvertHTML(document, options, savePath);

Save Options

Aspose.HTML allows converting HTML to GIF 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.
SmoothingMode This property sets the rendering quality for this image. Available values are Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias.
Text Gets a TextOptions object which is used for configuration of text rendering.

Convert HTML to GIF using ImageSaveOptions

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

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

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

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
using System.Drawing;
using System.Drawing.Drawing2D;
...
    string documentPath = Path.Combine(OutputDir, "convert-to-gif.html");
    string savePath = Path.Combine(OutputDir, "convert-to-gif-options.gif");

    // Prepare HTML code and save it to a file
    var code = "<h1> HTML to GIF Converter </h1>\r\n" +
               "<p> HTML to GIF conversion allows you to save an HTML document as a GIF image.</p>\r\n";
    
    File.WriteAllText(documentPath, code);
    
    // Initialize an  HTML Document from the html file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions(ImageFormat.Gif)
    {
        SmoothingMode = SmoothingMode.Default,
        HorizontalResolution = 100,
        VerticalResolution = 100,
        BackgroundColor = Color.MistyRose
    };
     options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 200), new Margin(30, 20, 10, 10));
    
    // Convert HTML to GIF
    Converter.ConvertHTML(document, options, savePath);   

The ImageSaveOptions() constructor initializes an instance of the ImageSaveOptions class that is passed to ConvertHTML() method. The ConvertHTML() method takes the document, options, output file path savePath and performs the conversion operation.

In the above example, we apply:

  • BackgroundColor property that sets the color that will fill the background. The default BackgroundColor is Transparent;
  • HorizontalResolution and VerticalResolution properties that set horizontal/vertical resolution for output images in pixels per inch. By default, these properties are 300 dpi;
  • SmoothingMode property that sets the rendering quality for this image.
  • PageSetup property that specifies the page size and margins.

The figure illustrates the convert-to-gif-options.gif file.

Text “convert-to-gif-options.gif image”

Text “Banner HTML to GIF Converter”