Convert EPUB to PNG in 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 for .NET, 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 for .NET 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.

                
            

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!

1// Invoke the ConvertEPUB() method to convert EPUB to PNG
2Converter.ConvertEPUB(File.OpenRead(DataDir + "input.epub"), new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));

Convert EPUB to PNG

To convert EPUB to PNG, 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.

 1// Open an existing EPUB file for reading
 2using var stream = File.OpenRead(DataDir + "input.epub");
 3
 4// Prepare a path to save the converted file
 5string savePath = Path.Combine(OutputDir, "input-output.png");
 6
 7// Create an instance of the ImageSaveOptions class 
 8var options = new ImageSaveOptions();
 9
10// Call the ConvertEPUB() method to convert EPUB to PNG
11Converter.ConvertEPUB(stream, options, savePath);

Save Options – ImageSaveOptions Class

Aspose.HTML for .NET 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.

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.
UseAntialiasingThis property sets the image rendering quality. Antialiasing is enabled by default.
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 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:

 1// Open an existing EPUB file for reading
 2using var stream = File.OpenRead(DataDir + "input.epub");
 3
 4// Prepare a path to save the converted file
 5string savePath = Path.Combine(OutputDir, "input-options.png");
 6
 7// Initialize ImageSaveOptions 
 8var options = new ImageSaveOptions()
 9{
10    UseAntialiasing = true,
11    HorizontalResolution = 400,
12    VerticalResolution = 400,
13    BackgroundColor = System.Drawing.Color.AliceBlue
14};            
15
16// Call the ConvertEPUB() method to convert EPUB to PNG
17Converter.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:

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.

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

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

Text “EPUB to PNG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.