用 C# 将 EPUB 转换为 GIF
GIF 是一种流行的图像格式,经常用于网络出版。使用 Aspose.HTML for .NET,您可以通过编程将 EPUB 转换为 GIF 格式,并完全控制各种转换参数。本文将介绍如何使用 Converter 类的 ConvertEPUB() 方法将 EPUB 转换为 GIF,以及如何应用 ImageSaveOptions 和 ICreateStreamProvider 参数。
在线 EPUB 转换器
您可以检查 Aspose.HTML API 功能并实时转换 EPUB。请从本地文件系统加载 EPUB 文件,选择输出格式并运行示例。在示例中,默认设置了保存选项。您将立即以单独文件的形式收到结果。
如果你想通过编程将 EPUB 转换为 GIF,请参阅以下 C# 代码示例。
将 EPUB 转换为 GIF
使用 Converter.ConvertEPUB()方法是将EPUB文件转换成各种格式的最常见方法。你应该遵循以下几个步骤:
- 打开现有 EPUB 文件。在示例中,我们使用 System.IO.FileStream 类的 OpenRead() 方法从指定路径的文件系统中打开并读取 EPUB 文件。
- 创建一个带有 GIF ImageFormat 的新 ImageSaveOptions 对象。默认情况下,格式属性为 PNG。
- 使用转换器类的 ConvertEPUB() 方法将 EPUB 保存为 GIF 图像。您需要向 ConvertEPUB() 方法传递 EPUB 文件流、ImageSaveOptions 和输出文件路径,以实现 EPUB 到 GIF 的转换。
请看下面的 C# 代码片段,其中显示了使用 Aspose.HTML for .NET 将 EPUB 转换为 GIF 的过程。
1// Convert EPUB to GIF 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.gif");
8
9// Create an instance of the ImageSaveOptions class
10ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
11
12// Call the ConvertEPUB() method to convert EPUB to GIF
13Converter.ConvertEPUB(stream, options, savePath);
保存选项 – ImageSaveOptions
Aspose.HTML 允许使用默认或自定义保存选项将 EPUB 转换为 GIF。使用 ImageSaveOptions 可以调整渲染过程。您可以指定页面大小、页边距、水平/垂直分辨率等。
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 转换为 GIF
要将 EPUB 转换为指定图像保存选项的 GIF,您需要遵循以下几个步骤:
- 打开现有 EPUB 文件
- 创建一个新的 ImageSaveOptions 对象并指定保存选项。
- 使用转换器类的 ConvertEPUB() 方法将 EPUB 保存为 GIF 图像。您需要向 ConvertEPUB() 方法传递 EPUB 文件流、ImageSaveOptions 和输出文件路径,以便将 EPUB 转换为 GIF。
下面的 C# 代码片段显示了如何使用自定义保存选项将 EPUB 转换为 GIF:
1// Convert EPUB to GIF 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 for converted file saving
7string savePath = Path.Combine(OutputDir, "input-options.gif");
8
9// Initialize ImageSaveOptions
10ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif)
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 GIF
20Converter.ConvertEPUB(stream, options, savePath);
ImageSaveOptions() 构造函数初始化 ImageSaveOptions 类的实例,并将其传递给 ConvertEPUB() 方法。ConvertEPUB() 方法接收 stream
、options
、输出文件路径 savePath
并执行转换操作。
在示例中,我们使用
HorizontalResolution
和VerticalResolution
属性以每英寸像素为单位设置输出图像的水平/垂直分辨率。默认情况下,这些属性为 300 dpi。UseAntialiasing
属性,用于设置该图像的渲染质量。BackgroundColor
属性,用于指定背景填充的颜色。默认的 BackgroundColor 是透明色。PageSetup
属性指定 page size 和 margins(以像素为单位)。
您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML提供了一个免费的在线 EPUB到GIF转换器,转换EPUB到GIF图像的质量高,方便,快捷。只需上传、转换文件并在几秒钟内获得结果!