用 C# 将 HTML 转换为 TIFF

使用 Converter.ConvertHTML 方法是将 HTML 代码转换为各种格式的最常用方法。例如,如果您想在 PowerPoint 演示文稿中添加网页或通过电子邮件发送网页,就可能需要将 HTML 文件转换为 TIFF 图像。使用 Aspose.HTML for .NET,您可以通过程序将 HTML 转换为 TIFF 格式,并完全控制各种转换参数。

通过 HTML 到 TIFF 的转换,您可以将 HTML 文档保存为 TIFF 图像。本文将介绍如何使用 Converter 类的 ConvertHTML() 方法将 HTML 转换为 TIFF,以及如何应用 ImageSaveOptionsICreateStreamProvider 参数。

在线 HTML 转换器

您可以检查 Aspose.HTML API 功能并实时转换 HTML。请从本地文件系统加载 HTML,选择输出格式并运行示例。在示例中,默认设置了保存选项。您将立即以单独文件的形式收到结果。

                
            

如果您想通过编程将 HTML 转换为 TIFF,请参阅以下 C# 代码示例。

将 HTML 转换为 TIFF

使用 ConvertHTML() 方法将文件转换为另一种格式是一系列操作,其中包括文件加载和保存:

  1. 使用 HTMLDocument 类加载 HTML 文件。
  2. 创建一个具有 TIFF 图像格式的新 ImageSaveOptions 对象。默认情况下,格式属性为 PNG
  3. 使用 Converter 类的 ConvertHTML() 方法将 HTML 保存为 TIFF 图像。您需要向 ConvertHTML() 方法传递 HTMLDocumentImageSaveOptions 和输出文件路径,以便将 HTML 转换为 TIFF。

请看下面的 C# 代码片段,它显示了使用 Aspose.HTML for .NET 将 HTML 转换为 TIFF 的过程。

 1// Convert HTML to TIFF using C#
 2
 3// Prepare a path to a source HTML file
 4string documentPath = Path.Combine(DataDir, "nature.html");
 5
 6// Prepare a path for converted file saving 
 7string savePath = Path.Combine(OutputDir, "nature-output.tiff");
 8
 9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create an instance of the ImageSaveOptions class 
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
14
15// Convert HTML to TIFF
16Converter.ConvertHTML(document, options, savePath);

ImageSaveOptions

Aspose.HTML 允许使用默认或自定义保存选项将 HTML 转换为 TIFF。使用 ImageSaveOptions 可以自定义渲染过程。您可以指定 image formatpage sizemarginsCompressionCSS media-type等。

PropertyDescription
CompressionSets Tagged Image File Format (TIFF) Compression. By default, this property is LZW.
CSSGets a CssOptions object which is used for configuration of CSS properties processing.
FormatSets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG.
BackgroundColorThis property sets the color that will fill the background. By default, this property is Transparent.
PageSetupThis property gets a page setup object and uses it for configuration output page-set.
HorizontalResolutionSets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolutionSets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
UseAntialiasingThis property sets the image rendering quality. Antialiasing is enabled by default.
TextGets a TextOptions object which is used for configuration of text rendering.

要了解有关 ImageSaveOptions 类的更多信息,请阅读 微调转换器 一文。

使用图像保存选项将 HTML 转换为 TIFF

要使用指定的 ImageSaveOptions 将 HTML 转换为 TIFF,需要遵循以下几个步骤:

  1. 使用 HTMLDocument 类的 HTMLDocument() 构造函数之一加载 HTML 文件。
  2. 使用 TIFF ImageFormat 创建一个新的 ImageSaveOptions 对象并指定保存选项。默认情况下,格式属性为 PNG
  3. 使用 Converter 类的 ConvertHTML() 方法将 HTML 保存为 TIFF 图像。您需要向 ConvertHTML() 方法传递 HTMLDocumentImageSaveOptions 和输出文件路径,以便将 HTML 转换为 TIFF。

以下 C# 代码片段展示了如何使用自定义保存选项将 HTML 转换为 TIFF:

 1// Convert HTML to TIFF with custom settings using C#
 2
 3string documentPath = Path.Combine(DataDir, "nature.html");
 4string savePath = Path.Combine(OutputDir, "nature-output-options.tiff");
 5
 6
 7// Initialize an HTML Document from the html file
 8using HTMLDocument document = new HTMLDocument(documentPath);
 9
10// Initialize ImageSaveOptions 
11ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff)
12{
13    Compression = Compression.None,
14    BackgroundColor = System.Drawing.Color.Bisque,
15    HorizontalResolution = 150,
16    VerticalResolution = 150,
17    UseAntialiasing = true,
18};
19
20// Convert HTML to TIFF
21Converter.ConvertHTML(document, options, savePath);

ImageSaveOptions() 构造函数初始化 ImageSaveOptions 类的实例,并将其传递给 ConvertHTML() 方法。ConvertHTML() 方法接收 document, options, 输出文件路径 savePath 并执行转换操作。

在上述示例中,我们添加

您可以从 GitHub 下载完整的示例和数据文件。

Aspose.HTML 提供免费的在线 HTML 到 TIFF 转换器,可将 HTML 转换为高质量的 TIFF 图像,简单快捷。只需上传、转换您的文件,几秒钟就能得到结果!

文本 “HTML 到 TIFF 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.