Convert EPUB to Image in .NET with Aspose.HTML

In today’s digital age, the ability to manipulate and convert various document formats is a valuable skill. Aspose.HTML for .NET is a powerful tool that allows developers to work with HTML and EPUB documents effortlessly. In this tutorial, we’ll delve into the world of Aspose.HTML for .NET and guide you through the process of converting EPUB documents to various image formats. We’ll break down each example into multiple steps, explaining every step along the way.

Prerequisites

Before we dive into the world of Aspose.HTML for .NET, you should ensure you have the following prerequisites in place:

  1. Visual Studio: Make sure you have Visual Studio installed on your system. You can download it from the website.

  2. Aspose.HTML for .NET: You can obtain the library from the Aspose website here.

  3. Your Data Directory: Prepare a directory where you store your EPUB files and where the output images will be saved.

  4. Basic Knowledge of C#: Familiarity with C# programming is essential to understand and implement the code examples provided in this tutorial.

Importing Necessary Namespaces

Before we start working with Aspose.HTML for .NET, you need to import the required namespaces into your C# code. These namespaces provide access to the Aspose.HTML for .NET features.

using Aspose.Html.Converters;
using Aspose.Html.Saving;
using Aspose.Html.Rendering.Image;
using Aspose.Html.IO;
using Aspose.Html.Drawing;
using System.IO;
using System.Drawing;
using System.Collections.Generic;

Now that we have the prerequisites and namespaces in place, let’s move on to the step-by-step examples.

Converting EPUB to JPEG

    string dataDir = "Your Data Directory";
    // Open an existing EPUB file for reading.
    using (var stream = File.OpenRead(Path.Combine(dataDir, "input.epub")))
    {
        // Call the ConvertEPUB method to convert the EPUB file to image.
        Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), "output.jpg");
    }

Steps

  1. Provide the path to your EPUB file in the dataDir variable.
  2. Open the EPUB file for reading using a FileStream.
  3. Call the ConvertEPUB method, passing the EPUB stream, an ImageSaveOptions specifying the output format (JPEG), and the output file name (“output.jpg”).
  4. The EPUB file is converted to a JPEG image.

In this example, we open an EPUB file, read its content, and convert it into a JPEG image format. The output image is saved as “output.jpg.”

Converting EPUB to PNG

You can easily convert EPUB files to various image formats like PNG, BMP, GIF, and TIFF using similar code structures. Here’s an example for converting to PNG:


    string dataDir = "Your Data Directory";
    using (var stream = File.OpenRead(Path.Combine(dataDir, "input.epub")))
    {
        var options = new ImageSaveOptions(ImageFormat.Png);
        Converter.ConvertEPUB(stream, options, "output.png");
    }

Steps

  1. Open the EPUB file for reading using a FileStream.
  2. Initialize an ImageSaveOptions object with the desired output format (in this case, PNG).
  3. Call the ConvertEPUB method, passing the EPUB stream, the image save options, and the output file name.
  4. The EPUB file is converted to the specified image format.

Specify Image Save Options

You can customize the image output by specifying options like page size and background color. Here’s an example:

    string dataDir = "Your Data Directory";
    using (var stream = File.OpenRead(Path.Combine(dataDir, "input.epub")))
    {
        var options = new ImageSaveOptions(ImageFormat.Jpeg)
        {
            PageSetup =
            {
                AnyPage = new Page()
                {
                    Size = new Size(Length.FromPixels(3000), Length.FromPixels(1000))
                }
            },
            BackgroundColor = Color.AliceBlue,
        };
        Converter.ConvertEPUB(stream, options, "output.jpg");
    }

Steps

  1. Provide the path to your EPUB file in the dataDir variable.
  2. Open the EPUB file for reading using a FileStream.
  3. Create an ImageSaveOptions object and specify the desired output format (JPEG).
  4. Customize the page size and background color, if needed.
  5. Call the ConvertEPUB method, passing the EPUB stream, the image save options, and the output file name.
  6. The EPUB file is converted to an image with the specified options.

Specify a Custom Stream Provider

If you need to manipulate the output stream, you can use a custom stream provider. Here’s an example:

    string dataDir = "Your Data Directory";
    using (var stream = File.OpenRead(Path.Combine(dataDir, "input.epub")))
    {
        using (var streamProvider = new MemoryStreamProvider())
        {
            Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
            
            for (int i = 0; i < streamProvider.Streams.Count; i++)
            {
                var memory = streamProvider.Streams[i];
                memory.Seek(0, SeekOrigin.Begin);
                
                using (FileStream fs = File.Create($"page_{i + 1}.jpg"))
                {
                    memory.CopyTo(fs);
                }
            }
        }
    }

MemoryStreamProvider Class Source Code.

class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
        {
            // List of MemoryStream objects created during the document rendering
            public List<System.IO.MemoryStream> Streams { get; } = new List<System.IO.MemoryStream>();
            public System.IO.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.
                System.IO.MemoryStream result = new System.IO.MemoryStream();
                Streams.Add(result);
                return result;
            }
            public System.IO.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 the image files (JPG, PNG, etc.)
                System.IO.MemoryStream result = new System.IO.MemoryStream();
                Streams.Add(result);
                return result;
            }
            public void ReleaseStream(System.IO.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();
            }
        }

Steps

  1. Provide the path to your EPUB file in the dataDir variable.
  2. Open the EPUB file for reading using a FileStream.
  3. Create a MemoryStreamProvider to handle custom output streams.
  4. Call the ConvertEPUB method, passing the EPUB stream, the image save options (JPEG), and the custom stream provider.
  5. Iterate through the memory streams in the custom provider, save them to individual files.
  6. This example allows you to manipulate and save multiple output streams as needed.

Conclusion

Aspose.HTML for .NET is a versatile library that simplifies working with EPUB and HTML documents. With the ability to convert EPUB documents to various image formats and customizable options, it offers a wide range of applications for developers.


Frequently Asked Questions

1. Where can I download Aspose.HTML for .NET?

You can download Aspose.HTML for .NET from the releases page here.

2. How can I get a temporary license for Aspose.HTML for .NET?

To obtain a temporary license, visit the temporary license page here.

3. Where can I find additional support for Aspose.HTML for .NET?

For any questions or issues, you can seek assistance from the Aspose community on the support forum here.

4. Can I convert EPUB documents to other formats like PDF or XPS?

Yes, you can use Aspose.HTML for .NET to convert EPUB documents to various formats, including PDF and XPS.

5. Is Aspose.HTML for .NET suitable for both small and large-scale projects?

Absolutely! Aspose.HTML for .NET is designed to be scalable, making it a great choice for projects of all sizes.