Convert EPUB to JPG – C# code examples
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 for .NET, 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 JPG using ConvertEPUB() methods of the Converter class, and how to apply ImageSaveOptions and ICreateStreamProvider parameters.
Online EPUB Converter
You can convert EPUB to other formats with Aspose.HTML for .NET API in real time. First, load an EPUB file from your local drive and then run the example. The save options in the example are set by default. You will immediately receive the conversion result as a separate file.
If you want to convert EPUB to JPG programmatically, please see the following C# code examples.
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!
1// Open an existing EPUB file for reading
2using var stream = File.OpenRead(DataDir + "input.epub");
3
4// Invoke the ConvertEPUB() method to convert EPUB to JPG
5Converter.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:
- 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.
- Create a new ImageSaveOptions object with JPG ImageFormat. By default, the Format property is PNG.
- 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.
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.jpg");
6
7// Create an instance of the ImageSaveOptions class
8var options = new ImageSaveOptions(ImageFormat.Jpeg);
9
10// Call the ConvertEPUB() method to convert EPUB to JPG
11Converter.ConvertEPUB(stream, options, savePath);
You can download the complete examples and data files from GitHub.
Save Options
Aspose.HTML for .NET 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. |
UseAntialiasing | This property sets the image rendering quality. Antialiasing is enabled by default. |
Text | Gets 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 JPG using ImageSaveOptions
To convert EPUB to JPG with ImageSaveOptions specifying, you should follow a few steps:
- Open an existing EPUB file.
- Create a new ImageSaveOptions object with JPG ImageFormat and specify save options. By default, the Format property is PNG.
- 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:
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.jpg");
6
7// Initialize ImageSaveOptions
8var options = new ImageSaveOptions(ImageFormat.Jpeg)
9{
10 UseAntialiasing = true,
11 HorizontalResolution = 400,
12 VerticalResolution = 400,
13 BackgroundColor = System.Drawing.Color.AliceBlue
14};
15options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(800, 500), new Margin(30, 20, 10, 10));
16
17// Call the ConvertEPUB() method to convert EPUB to JPG
18Converter.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
andVerticalResolution
properties that set horizontal/vertical resolution for output images in pixels per inch. By default, these properties are 300 dpi.UseAntialiasing
property that sets the rendering quality for this image. This example usesUseAntialiasing = true
for quality rendering.PageSetup
property that specifies the page size and margins in pixels.
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
.
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.
Aspose.HTML for .NET provides various types of output formats for rendering operations. Some of these formats produce a single output file (for instance, PDF, XPS), others create multiple files (Image formats – JPG, PNG, etc.).
The example below shows how to implement and use your own MemoryStreamProvider in the application:
1class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
2{
3 // List of MemoryStream objects created during the document rendering
4 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
5
6 public Stream GetStream(string name, string extension)
7 {
8 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
9 MemoryStream result = new MemoryStream();
10 Streams.Add(result);
11 return result;
12 }
13
14 public Stream GetStream(string name, string extension, int page)
15 {
16 // 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.)
17 MemoryStream result = new MemoryStream();
18 Streams.Add(result);
19 return result;
20 }
21
22 public void ReleaseStream(Stream stream)
23 {
24 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
25 }
26
27 public void Dispose()
28 {
29 // Releasing resources
30 foreach (var stream in Streams)
31 stream.Dispose();
32 }
33}
The following code snippet demonstrates how to convert an EPUB file to a JPG file using a memory stream.
1// Open an existing EPUB file for reading
2using var stream = File.OpenRead(DataDir + "input.epub");
3
4// Create an instance of MemoryStreamProvider
5using var streamProvider = new MemoryStreamProvider();
6
7// Convert HTML to JPG using the MemoryStreamProvider
8Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
9
10// Get access to the memory streams that contain the resulted data
11for (int i = 0; i < streamProvider.Streams.Count; i++)
12{
13 var memory = streamProvider.Streams[i];
14 memory.Seek(0, System.IO.SeekOrigin.Begin);
15
16 // Flush the page to the output file
17 using (FileStream fs = File.Create(Path.Combine(OutputDir, $"input-page_{i + 1}.jpg")))
18 {
19 memory.CopyTo(fs);
20 }
21}
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.
Aspose.HTML offers a free online EPUB to JPG Converter that converts EPUB to JPG image with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!