Convert EPUB to PNG | C#

PNG file format supports lossless image compression that makes it popular among its users. It widely takes to transmit pictures over the network, display photos and graphics on web pages and reserve in cloud storages. With Aspose.HTML, you can convert EPUB to PNG format programmatically with full control over a wide range of conversion parameters.

In this article, you find information on how to convert EPUB to PNG using ConvertEPUB() methods of the Converter class, and how to apply ImageSaveOptions and ICreateStreamProvider parameters.

Online EPUB Converter

You can check the Aspose.HTML API functionality and convert EPUB in real-time. Please load an EPUB file 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 EPUB to PNG programmatically, please see the following C# code examples.

EPUB to PNG by a single line of code

The static methods of the Converter class are primarily used as the easiest way to convert an EPUB file into various formats. You can convert EPUB to PNG in your C# application literally with a single line of code!

using System.IO;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
...
    // Invoke the ConvertEPUB method to convert the EPUB code to PNG image           
    Converter.ConvertEPUB(File.OpenRead(DataDir + "input.epub"), new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));

Convert EPUB to PNG

Using Converter.ConvertEPUB methods is the most common way to convert EPUB files into various formats. You should follow a few steps:

  1. Open an existing EPUB file. In the example, we use the OpenRead() method of System.IO.FileStream class to open and read an EPUB file from the file system at the specified path.
  2. Create a new ImageSaveOptions object. By default, the Format property is PNG.
  3. Use the ConvertEPUB() method of the Converter class to save EPUB as a PNG image. You need to pass the EPUB file stream, ImageSaveOptions, and output file path to the ConvertEPUB() method for EPUB to PNG conversion.

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

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
...
   // Open an existing EPUB file for reading
   using var stream = File.OpenRead(DataDir + "input.epub");

   // Prepare a path for converted file saving 
   string savePath = Path.Combine(OutputDir, "input-output.png");

   // Initialize ImageSaveOptions 
   var options = new ImageSaveOptions();

   // Call the ConvertEPUB method to convert EPUB to PNG
   Converter.ConvertEPUB(stream, options, savePath);

Save Options

Aspose.HTML allows converting EPUB to PNG using default or custom save options. ImageSaveOptions usage enables you to tune the rendering process. You can specify the page size, margins, 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 EPUB to PNG using ImageSaveOptions

To convert EPUB to PNG with ImageSaveOptions specifying, you should follow a few steps:

  1. Open an existing EPUB file.
  2. Create a new ImageSaveOptions object and specify save options.
  3. Use the ConvertEPUB() method of the Converter class to save EPUB as a PNG image. You need to pass the EPUB file stream, ImageSaveOptions, and output file path to the ConvertEPUB() method to convert EPUB to PNG.

The following C# code snippet shows how to convert EPUB to PNG 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 Aspose.Html.Drawing;
using System.Drawing.Drawing2D;
...
    // Open an existing EPUB file for reading
    using var stream = File.OpenRead(DataDir + "input.epub");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "input-options.png");
    
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions()
    {
        SmoothingMode = SmoothingMode.HighQuality,
        HorizontalResolution = 400,
        VerticalResolution = 400,
        BackgroundColor = Color.AliceBlue
    };            
    
    // Call the ConvertEPUB method to convert EPUB to PNG
    Converter.ConvertEPUB(stream, options, savePath);

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

In the example, we use:

  • BackgroundColor property that specifies the color that the background will be filled in. 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.

Text “Banner EPUB to PNG Converter”