用 C# 将 MHTML 转换为图像

本文将介绍如何将 MHTML 文件转换为图像文件格式,如 JPGPNGBMPTIFFGIF,以及如何使用 ImageSaveOptionsICreateStreamProvider 参数。

在线 MHTML 转换器

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

                
            

如果您想通过编程将 MHTML 转换为图像格式,请参阅以下 C# 代码示例。

通过两行代码将 MHTML 转换为 JPG

Converter 类的静态方法主要用于将 MHTML 文件转换为各种格式的最简单方法。只需两行代码,您就可以在 C# 应用程序中将 MHTML 转换为 JPG!

1// Convert MHTML to JPG in C#
2
3// Open an existing MHTML file for reading
4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
5
6// Invoke the ConvertMHTML() method to convert MHTML to JPG
7Converter.ConvertMHTML(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-by-two-lines.jpg"));

将 MHTML 转换为 JPG

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

  1. 打开现有的 MHTML 文件。
  2. 创建一个新的带有 ImageFormat.JpegImageSaveOptions 对象。默认情况下,格式属性为 PNG。在此,您可以设置所需的保存选项,如页面设置或分辨率。
  3. 使用 Converter 类的 ConvertMHTML() 方法将 MHTML 保存为 JPG 图像。您需要向 ConvertMHTML() 方法传递 MHTML 文件流、ImageSaveOptions 和输出文件路径,以便将 MHTML 转换为 JPG。

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

 1// Convert MHTML to JPG using C#
 2
 3// Open an existing MHTML file for reading
 4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 5            
 6// Create an instance of ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
 8
 9// Call the ConvertMHTML() method to convert MHTML to JPG
10Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.jpg"));

将 MHTML 转换为 PNG

下面的 C# 代码片段展示了如何将 MHTML 转换为 PNG。您应遵循以下几个步骤

  1. 打开现有的 MHTML 文件。
  2. 创建一个新的 ImageSaveOptions 对象。默认情况下,格式属性为 PNG。在此,您可以设置所需的保存选项,如页面设置或分辨率。
  3. 使用 Converter 类的 ConvertMHTML() 方法将 MHTML 保存为 PNG 图像。您需要向 ConvertMHTML() 方法传递 MHTML 文件流、ImageSaveOptions 和输出文件路径,以便将 MHTML 转换为 PNG。

下面的 C# 代码片段展示了如何使用 Aspose.HTML for .NET 将 MHTML 转换为 PNG。

 1// Convert MHTML to PNG using C#
 2
 3// Open an existing MHTML file for reading
 4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 5
 6// Create an instance of ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
 8
 9// Call the ConvertMHTML() method to convert MHTML to PNG
10Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.png"));

将 MHTML 转换为 BMP

下面的 C# 代码片段展示了如何将 MHTML 转换为 BMP。您应遵循以下几个步骤

  1. 打开现有的 MHTML 文件。
  2. 创建一个新的 ImageSaveOptions 对象,其属性为 ImageFormat.Bmp。默认情况下,格式属性为 PNG
  3. 使用 Converter 类的 ConvertMHTML() 方法将 MHTML 保存为 BMP 图像。您需要向 ConvertMHTML() 方法传递 MHTML 文件流、ImageSaveOptions 和输出文件路径,以便将 MHTML 转换为 BMP。

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

 1// Convert MHTML to BMP using C#
 2
 3// Open an existing MHTML file for reading
 4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 5
 6// Create an instance of ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
 8
 9// Call the ConvertMHTML() method to convert MHTML to BMP
10Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.bmp"));

将 MHTML 转换为 GIF

要将 MHTML 转换为 GIF,您需要遵循几个步骤:

  1. 打开现有的 MHTML 文件。
  2. 创建一个带有 GIF ImageFormat 的新 ImageSaveOptions 对象。默认情况下,格式属性为 PNG
  3. 使用 Converter 类的 ConvertMHTML() 方法将 MHTML 保存为 GIF 图像。您需要向 ConvertMHTML() 方法传递 MHTML 文件流、ImageSaveOptions 和输出文件路径,以便将 MHTML 转换为 GIF。

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

 1// Convert MHTML to GIF in C#
 2
 3// Open an existing MHTML file for reading
 4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 5
 6// Create an instance of ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
 8
 9// Call the ConvertMHTML() method to convert MHTML to GIF
10Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.gif"));

将 MHTML 转换为 TIFF

使用 ConvertMHTML() 方法将文件转换为另一种格式是一系列操作,其中包括文档加载和保存。要将 MHTML 转换为 TIFF,应遵循以下几个步骤:

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

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

 1// Convert MHTML to TIFF in C#
 2
 3// Open an existing MHTML file for reading
 4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "sample-options.tiff");
 8
 9// Create an instance of ImageSaveOptions
10ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
11
12// Call the ConvertMHTML() method to convert MHTML to TIFF
13Converter.ConvertMHTML(stream, options, savePath);

ImageSaveOptions

Aspose.HTML 允许使用默认或自定义保存选项将 MHTML 转换为图像。使用 ImageSaveOptions 可以调整渲染过程。您可以指定 image formatpage sizemarginsCSS 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 rendering quality for this image. Available values are Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias.
TextGets a TextOptions object which is used for configuration of text rendering.

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

使用 ImageSaveOptions 将 MHTML 转换为 JPG

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

  1. 打开现有的 MHTML 文件。
  2. 使用 JPG ImageFormat 创建一个新的 ImageSaveOptions 对象,并指定保存选项。默认情况下,格式属性为 PNG
  3. 使用转换器类的 ConvertMHTML() 方法将 MHTML 保存为 JPG 图像。

ImageSaveOptions 类提供了大量属性,可让您完全控制各种参数,并改进将 MHTML 转换为图像格式的过程。以下 C# 代码片段展示了如何使用自定义保存选项将 MHTML 转换为 JPG:

 1// Convert MHTML to JPG in C# with custom settings
 2
 3// Open an existing MHTML file for reading
 4using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "sample-options.jpg");
 8
 9// Initailize the ImageSaveOptions with a custom page-size and a background color
10ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
11{
12    PageSetup =
13        {
14            AnyPage = new Page()
15            {
16                Size = new Aspose.Html.Drawing.Size(Length.FromPixels(1000), Length.FromPixels(500))
17            }
18        },
19    BackgroundColor = System.Drawing.Color.Beige
20};
21
22// Call the ConvertMHTML() method to convert MHTML to JPG
23Converter.ConvertMHTML(stream, options, savePath);

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

在示例中,我们使用

输出流提供商

如果需要将文件保存在远程存储器(如云、数据库等)中,可以实现 ICreateStreamProvider接口来手动控制文件创建过程。该接口被设计为一个回调对象,用于在文档/页面开始时创建一个流(取决于输出格式),并在渲染文档/页面后释放早期创建的流。

Aspose.HTML for .NET为渲染操作提供了多种输出格式。其中一些格式只生成一个输出文件(例如 PDFXPS),其他格式则生成多个文件(图像格式 JPGPNG等)。

下面的示例展示了如何在应用程序中实现和使用自己的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}
 1// Convert MHTML to JPG in C# using memory stream
 2
 3// Create an instance of MemoryStreamProvider
 4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Open an existing MHTML file for reading
 7using FileStream stream = File.OpenRead(DataDir + "sample.mht");
 8
 9// Prepare a path to save the converted file
10string savePath = Path.Combine(OutputDir, "stream-provider.jpg");
11
12// Convert MHTML to JPG by using the MemoryStreamProvider class
13Converter.ConvertMHTML(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
14
15// Get access to the memory streams that contain the resulted data
16for (int i = 0; i < streamProvider.Streams.Count; i++)
17{
18    MemoryStream memory = streamProvider.Streams[i];
19    memory.Seek(0, SeekOrigin.Begin);
20
21    // Flush the page to the output file
22    using (FileStream fs = File.Create(savePath))
23    {
24        memory.CopyTo(fs);
25    }
26}

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

下载 Aspose.HTML for .NET库,成功、快速、轻松地将您的 HTML、MHTML、EPUB、SVG 和 Markdown 文档转换为最流行的格式。

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

文本 “MHTML 到 JPG 转换器”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.