Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
JPG 文件可以无损压缩的方式包含高质量的图像数据。这种独特的压缩功能可以快速高效地共享 JPG 图像,并在网络、电脑和移动设备上广泛使用。使用 Aspose.HTML for .NET,您可以通过编程将 EPUB 转换为 JPG 格式,并完全控制各种转换参数。
本文将介绍如何使用 Converter 类的 ConvertEPUB() 方法将 EPUB 转换为 JPG,以及如何应用 ImageSaveOptions 和 ICreateStreamProvider 参数。
您可以使用 Aspose.HTML for .NET API 实时将 EPUB 转换为其他格式。首先,从本地驱动器加载 EPUB 文件,然后运行示例。示例中的保存选项为默认设置。您将立即以单独文件的形式收到转换结果。
如果您想以编程方式将 EPUB 转换为 JPG,请参阅以下 C# 代码示例。
Converter 类的静态方法主要用于将 EPUB 文件转换为各种格式。只需两行代码,您就可以在 C# 应用程序中将 EPUB 转换为 JPG!
1// Convert EPUB to JPG using C#
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Invoke the ConvertEPUB() method to convert EPUB to JPG
7Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-by-two-lines.jpg"));要将 EPUB 转换为 JPG,应遵循以下几个步骤:
ImageFormat.Jpeg 的
ImageSaveOptions 对象。默认情况下,格式属性为
PNG。请看下面的 C# 代码片段,它显示了使用 Aspose.HTML for .NET 将 EPUB 转换为 JPG 的过程。
1// Convert EPUB to JPG using C#
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "input-output.jpg");
8
9// Create an instance of the ImageSaveOptions class
10ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
11
12// Call the ConvertEPUB() method to convert EPUB to JPG
13Converter.ConvertEPUB(stream, options, savePath);您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML for .NET 允许使用默认或自定义保存选项将 EPUB 转换为 JPG。使用 ImageSaveOptions 可以调整渲染过程。您可以指定 image format、 page size、 margins、 CSS media-type 等。
| 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. |
要了解有关 ImageSaveOptions 类的更多信息,请阅读 微调转换器 一文。
要将 EPUB 转换为 JPG 并指定 ImageSaveOptions,您需要遵循以下几个步骤:
下面的 C# 代码片段显示了如何使用自定义保存选项将 EPUB 转换为 JPG:
1// Convert EPUB to JPG using C# with custom settings
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "input-options.jpg");
8
9// Initialize ImageSaveOptions
10ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
11{
12 UseAntialiasing = true,
13 HorizontalResolution = 400,
14 VerticalResolution = 400,
15 BackgroundColor = System.Drawing.Color.AliceBlue
16};
17options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(800, 500), new Margin(30, 20, 10, 10));
18
19// Call the ConvertEPUB() method to convert EPUB to JPG
20Converter.ConvertEPUB(stream, options, savePath);ImageSaveOptions() 构造函数初始化 ImageSaveOptions 类的实例,并将其传递给 ConvertEPUB() 方法。ConvertEPUB() 方法接收 stream、options、输出文件路径 savePath 并执行转换操作。
在上述示例中,我们使用
BackgroundColor 属性,用于指定背景填充的颜色。默认的 BackgroundColor 是透明色。HorizontalResolution 和 VerticalResolution 属性以每英寸像素为单位设置输出图像的水平/垂直分辨率。默认情况下,这些属性为 300 dpi。UseAntialiasing 属性,用于设置此图像的渲染质量。本示例使用 UseAntialiasing = true 设置渲染质量。PageSetup 属性指定
page size 和
margins(以像素为单位)。如果您想提高应用程序中渲染的图形、文本和图像的视觉质量,尤其是对清晰度和平滑边缘要求较高时,请使用 UseAntialiasing = true。启用抗锯齿功能可通过混合边缘周围像素的颜色来平滑锯齿状边缘,从而获得更柔和、更精致的外观。
虽然 UseAntialiasing = true 可以提供更好的视觉质量,但也会增加处理时间。对于优先考虑渲染速度的应用程序,设置 UseAntialiasing = false 可能是最佳选择。
如果需要将文件保存在远程存储器(如云、数据库等)中,可以实现 ICreateStreamProvider 接口来手动控制文件创建过程。该接口被设计为一个回调对象,用于在文档/页面开始时创建一个流(取决于输出格式),并在渲染文档/页面后释放早期创建的流。
下面的示例展示了如何在应用程序中实现和使用自己的MemoryStreamProvider:
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // 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.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}下面的代码片段演示了如何使用内存流将 EPUB 文件转换为 JPG 文件。
1// Convert EPUB to JPG in C# using memory stream
2
3// Open an existing EPUB file for reading
4using FileStream stream = File.OpenRead(DataDir + "input.epub");
5
6// Create an instance of MemoryStreamProvider
7using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
8
9// Convert HTML to JPG using the MemoryStreamProvider
10Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
11
12// Get access to the memory streams that contain the resulted data
13for (int i = 0; i < streamProvider.Streams.Count; i++)
14{
15 MemoryStream memory = streamProvider.Streams[i];
16 memory.Seek(0, System.IO.SeekOrigin.Begin);
17
18 // Flush the page to the output file
19 using (FileStream fs = File.Create(Path.Combine(OutputDir, $"input-page_{i + 1}.jpg")))
20 {
21 memory.CopyTo(fs);
22 }
23}ConvertEPUB(Stream, ImageSaveOptions, ICreateStreamProvider) 方法接收转换源、选项和用于获取输出流的 MemoryStreamProvider 实例,并执行转换操作。
Aspose.HTML提供了一个免费的在线 EPUB到JPG转换器,转换EPUB到JPG图像的高品质,方便,快捷。只需上传、转换您的文件并在几秒钟内获得结果!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.