Convert EPUB to JPG | C#

JPG files can contain high-quality image data with lossless compression. This unique compression feature allows to quickly and efficiently share JPG images and use them widely on the Web, computers, and mobile devices. With Aspose.HTML, you can convert EPUB to JPG format programmatically with full control over a wide range of conversion parameters.

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

Text “Banner EPUB to JPG Converter”

EPUB to JPG by two lines 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 JPG in your C# application literally with two lines of code!

using System.IO;
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");

    // Invoke the ConvertEPUB method to convert the EPUB code to JPG image           
    Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-by-two-lines.jpg"));

Convert EPUB to JPG

To convert EPUB to JPG, 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 with JPG ImageFormat. By default, the Format property is PNG.
  3. Use the ConvertEPUB() method of the Converter class to save EPUB as a JPG image. You need to pass the EPUB file stream, ImageSaveOptions, and output file path to the ConvertEPUB() method for EPUB to JPG conversion.

Please take a look over the following C# code snippet which shows the process of converting EPUB to JPG 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.jpg");            
    
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions(ImageFormat.Jpeg);
    
    // Call the ConvertEPUB method to convert EPUB to JPG
    Converter.ConvertEPUB(stream, options, savePath);

Save Options

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

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

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

The following C# code snippet shows how to convert EPUB to JPG 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.jpg");
                
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions(ImageFormat.Jpeg)
    {
        SmoothingMode = SmoothingMode.HighQuality,
        HorizontalResolution = 400,
        VerticalResolution = 400,
        BackgroundColor = Color.AliceBlue
    };
    options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(800, 500), new Margin(30, 20, 10, 10));
    
    // Call the ConvertEPUB method to convert EPUB to JPG
    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 above 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;
  • PageSetup property that specifies the page size and margins in pixels.

The ImageSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the EPUB to JPG conversion process. 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.

Output Stream Providers

If it is required to save files in the remote storage (e.g., cloud, database, etc.) you can implement the ICreateStreamProvider interface to have manual control over the file creating process. This interface is designed as a callback object to create a stream at the beginning of the document/page (depending on the output format) and release the early created stream after rendering the document/page.

The example below shows how to implement and use your own MemoryStreamProvider in the application:

using System.IO;
using System.Collections.Generic;
...
    class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
    {
        // List of MemoryStream objects created during the document rendering
        public List<MemoryStream> Streams { get; } = new List<MemoryStream>();

        public Stream GetStream(string name, string extension)
        {
            // This method is called when the only one output stream is required, for instance for XPS, PDF or TIFF formats.
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }
    
        public Stream GetStream(string name, string extension, int page)
        {
            // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }
    
        public void ReleaseStream(Stream stream)
        {
            //  Here you can release the stream filled with data and, for instance, flush it to the hard-drive
        }
    
        public void Dispose()
        {
            // Releasing resources
            foreach (var stream in Streams)
                stream.Dispose();
        }
    }

using System.IO;
using Aspose.Html;
using System.Linq;
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");

    // Create an instance of MemoryStreamProvider
    using var streamProvider = new MemoryStreamProvider();
    
    // Convert HTML to JPG using the MemoryStreamProvider
    Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
    
    // Get access to the memory streams that contain the resulted data
    for (int i = 0; i < streamProvider.Streams.Count; i++)
    {
        var memory = streamProvider.Streams[i];
        memory.Seek(0, System.IO.SeekOrigin.Begin);
    
        // Flush the page to the output file
        using (FileStream fs = File.Create(Path.Combine(OutputDir, $"input-page_{i + 1}.jpg")))
        {
            memory.CopyTo(fs);
        }

The ConvertEPUB(Stream, ImageSaveOptions, ICreateStreamProvider) method takes the conversion source, options, and instance of MemoryStreamProvider, which will be used to get an output stream, and performs the conversion operation.