用 C# 将 EPUB 转换为 PNG
PNG 文件格式支持无损图像压缩,因此深受用户欢迎。它广泛用于在网络上传输图片、在网页上显示照片和图形以及在云存储中保留图片。有了 Aspose.HTML for .NET,你可以通过编程将 EPUB 转换为 PNG 格式,并完全控制各种转换参数。
本文将介绍如何使用 Converter 类的 ConvertEPUB() 方法将 EPUB 转换为 PNG,以及如何应用 ImageSaveOptions 和 ICreateStreamProvider 参数。
在线 EPUB 转换器
您可以检查 Aspose.HTML for .NET API 的功能,并实时转换 EPUB。请从本地文件系统加载 EPUB 文件,选择输出格式并运行示例。在示例中,默认设置了保存选项。您将立即收到一个单独文件的结果。
如果你想通过编程将 EPUB 转换为 PNG,请参阅以下 C# 代码示例。
用一行代码将 EPUB 转换为 PNG
Converter 类的静态方法主要用于将 EPUB 文件转换为各种格式。只需一行代码,您就可以在 C# 应用程序中将 EPUB 转换为 PNG!
1// Convert EPUB to PNG using C#
2
3// Invoke the ConvertEPUB() method to convert EPUB to PNG
4Converter.ConvertEPUB(File.OpenRead(DataDir + "input.epub"), new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));
将 EPUB 转换为 PNG
要将EPUB转换为PNG,应遵循以下几个步骤:
- 打开现有 EPUB 文件。在示例中,我们使用 System.IO.FileStream 类的 OpenRead() 方法从指定路径的文件系统中打开并读取 EPUB 文件。
- 创建一个新的 ImageSaveOptions 对象。默认情况下,格式属性为 PNG。
- 使用转换器类的 ConvertEPUB()方法将 EPUB 保存为 PNG 图像。您需要向 ConvertEPUB() 方法传递 EPUB 文件流、ImageSaveOptions 和输出文件路径,以便将 EPUB 转换为 PNG。
请看下面的 C# 代码片段,其中显示了使用 Aspose.HTML for .NET 将 EPUB 转换为 PNG 的过程。
1// Convert EPUB to PNG in 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.png");
8
9// Create an instance of the ImageSaveOptions class
10ImageSaveOptions options = new ImageSaveOptions();
11
12// Call the ConvertEPUB() method to convert EPUB to PNG
13Converter.ConvertEPUB(stream, options, savePath);
保存选项 – ImageSaveOptions
Aspose.HTML for .NET 允许使用默认或自定义保存选项将 EPUB 转换为 PNG。使用 ImageSaveOptions 可以调整渲染过程。您可以指定 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 转换为 PNG
要将EPUB转换为指定ImageSaveOptions的PNG,你应该遵循以下几个步骤:
- 打开现有 EPUB 文件
- 创建一个新的 ImageSaveOptions 对象并指定保存选项。
- 使用转换器类的 ConvertEPUB()方法将 EPUB 保存为 PNG 图像。您需要向 ConvertEPUB() 方法传递 EPUB 文件流、ImageSaveOptions 和输出文件路径,以便将 EPUB 转换为 PNG。
下面的 C# 代码片段显示了如何使用自定义保存选项将 EPUB 转换为 PNG:
1// Convert EPUB to PNG in 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.png");
8
9// Initialize ImageSaveOptions
10ImageSaveOptions options = new ImageSaveOptions()
11{
12 UseAntialiasing = true,
13 HorizontalResolution = 400,
14 VerticalResolution = 400,
15 BackgroundColor = System.Drawing.Color.AliceBlue
16};
17
18// Call the ConvertEPUB() method to convert EPUB to PNG
19Converter.ConvertEPUB(stream, options, savePath);
ImageSaveOptions() 构造函数初始化 ImageSaveOptions 类的实例,并将其传递给 ConvertEPUB() 方法。ConvertEPUB() 方法接收 stream
、options
、输出文件路径 savePath
并执行转换操作。
在示例中,我们使用
BackgroundColor
属性,用于指定背景填充的颜色。默认的 BackgroundColor 是透明色。HorizontalResolution
和VerticalResolution
属性以每英寸像素为单位设置输出图像的水平/垂直分辨率。默认情况下,这些属性为 300 dpi。UseAntialiasing
属性,用于设置此图像的渲染质量。本示例使用UseAntialiasing = true
设置渲染质量。
如果您想提高应用程序中渲染的图形、文本和图像的视觉质量,尤其是对清晰度和平滑边缘要求较高时,请使用 UseAntialiasing = true
。启用抗锯齿功能可通过混合边缘像素的颜色来平滑锯齿状边缘,从而使外观更柔和、更精致。
虽然 UseAntialiasing = true
可以提供更好的视觉质量,但也会增加处理时间。对于优先考虑渲染速度的应用程序,设置 UseAntialiasing = false
可能是最佳选择。
您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML提供了一个免费的在线 EPUB到PNG转换器,转换EPUB到PNG图像质量高,方便,快捷。只需上传、转换文件并在几秒钟内获得结果!