Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert MHTML to image formats in C#, open the MHTML file as a stream, create ImageSaveOptions, choose the output image format, and call Converter.ConvertMHTML() with the stream, options, and output path or stream provider.
MHTML stores a web page and its resources in a single archive. MHTML to image conversion is useful for previews, thumbnails, visual archives, QA snapshots, and systems that need raster output instead of a document file.
Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.
The static
Converter class provides the shortest path from an MHTML stream to an image file. The example below converts MHTML to JPG with ImageSaveOptions.
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"));Use JPG output when you need compact previews or thumbnails and small file size matters more than transparency or lossless text edges.
The workflow is:
File.OpenRead() or another readable stream.ImageSaveOptions instance with ImageFormat.Jpeg.Converter.ConvertMHTML(stream, options, savePath). 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"));PNG is the default ImageSaveOptions.Format value and is often a good choice for sharp previews. The following example converts MHTML to 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"));BMP can be useful for Windows-oriented or legacy image workflows. The following example converts MHTML to 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"));GIF can be useful for simple compatibility-focused output. The following example converts MHTML to 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"));TIFF can be useful for archival, printing, scanning, and document imaging workflows. The following example converts MHTML to 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);Use ImageSaveOptions when the generated image needs custom rendering settings.
To customize MHTML to image conversion:
ImageSaveOptions and choose the output format.PageSetup, BackgroundColor, HorizontalResolution, VerticalResolution, UseAntialiasing, or TIFF Compression.Converter.ConvertMHTML().| Option | Use it to control |
|---|---|
Format | The output image format. The default value is PNG; use ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Gif, or ImageFormat.Tiff when another format is required. |
BackgroundColor | The color used to fill the image background. The default value is transparent. |
PageSetup | Page size, margins, and page layout settings. |
HorizontalResolution and VerticalResolution | Output resolution values used by the rendering process. The default value is 300 dpi for each property. |
UseAntialiasing | Image rendering quality. Antialiasing is enabled by default. |
Text | Text rendering configuration through TextOptions. |
Compression | TIFF compression. Use it only for TIFF output; the default value is LZW. |
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);Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, and services that decide where rendered image output is stored.
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}The following example converts MHTML to JPG with MemoryStreamProvider.
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}Use the online MHTML to JPG Converter for quick manual conversion. Use Aspose.HTML for .NET when MHTML to image conversion must run inside a C# application, service, or document pipeline.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.