Convert MHTML to JPG in Java

Converting MHTML to image file formats may be helpful, for example, when you want to save a copy of a web page for archiving or share the content of a web page with someone who doesn’t have Internet access or to embed a screenshot of a web page into other documents, such as presentations or reports.

Aspose.HTML for Java library provides a wide range of MHTML conversions to images, such as JPG, PNG, BMP, TIFF, and GIF. Converting MHTML to any supported image format follows the same mandatory steps:

The only difference is in specifying the output image format using the ImageSaveOptions class. The ImageSaveOptions(format) constructor initializes the options object with the format specifying. You can set the image format to JPG, PNG, BMP, GIF, or TIFF. The default ImageFormat is PNG.

In this article, you will find information on how to convert an MHTML to JPG using Aspose.HTML for Java and how to apply ImageSaveOptions and MemoryStreamProvider. You can easily use Java examples to convert MHTML to JPG, detailed here for converting MHTML to PNG, MHTML to BMP, MHTML to GIF, and MHTML to TIFF. Just set the ImageFormat to ImageSaveOptions!

MHTML to JPG by a few lines of Java code

The static methods of the Converter class are primarily used as the easiest way to convert an MHTML file into various formats. You can convert MHTML to JPG in your Java application literally with a few lines of code!

1    // Open an existing MHTML file for reading
2    final  FileStream stream = File.openRead(StringExtensions.concat(getDataDir(),  "sample.mht"));
3    try {        }
4    finally { if (stream != null) ((IDisposable)stream).dispose(); }
5
6    // Invoke the convertMHTML() method to convert MHTML to JPG
7    com.aspose.html.converters.Converter.convertMHTML(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.combine(getOutputDir(), "convert-by-few-lines.jpg"));

Convert MHTML to JPG

The JPG format is known for its high-quality image data with lossless compression, which makes it a popular choice for sharing images on the web, computers, and mobile devices. Using Aspose.HTML, you can easily convert MHTML files to JPG format with full control over various conversion parameters.

Let’s walk through the step-by-step instructions for a simple MHTML to JPG conversion scenario:

  1. Open an existing MHTML file. In the example, we use the openRead() method to open and read an MHTML file from the file system at the specified path.
  2. Create an instance of ImageSaveOptions with JPG ImageFormat. By default, the ImageFormat is PNG.
  3. Use the convertMHTML(stream, options, savePath) method of the Converter class to save MHTML as a JPG image file. The method takes as parameters stream, options, and savePath and performs the conversion.

Using convertMHTML() methods is the most common way to convert MHTML files into various formats. The following Java code snippet shows how to convert MHTML to JPG using Aspose.HTML:

 1    // Open an existing MHTML file for reading
 2    final  FileStream stream = File.openRead(StringExtensions.concat(getDataDir(),  "sample.mht"));
 3    try {        }
 4    finally { if (stream != null) ((IDisposable)stream).dispose(); }
 5                    
 6    // Create an instance of ImageSaveOptions
 7    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
 8
 9    // Prepare a path to save the converted file 
10    String savePath = Path.combine(getOutputDir(), "sample-output.jpg");
11
12    // Convert MHTML to JPG
13    com.aspose.html.converters.Converter.convertMHTML(stream, options, savePath);

You can download the complete examples and data files from GitHub.

Save Options

Aspose.HTML for Java provides the ability to programmatically convert MHTML to JPG with full control over various conversion options using ImageSaveOptions. This feature allows users to customize the rendering process by setting the image format, page size, margins, compression level, CSS media-type, and other parameters.

MethodDescription
setCompression(value)Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW.
getCssGets a CssOptions object which is used for configuration of CSS properties processing.
setFormat(value)Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png.
setBackgroundColor(value)Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()).
setPageSetup(value)Gets a page setup object is used for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setSmoothingMode(value)Sets the rendering quality for this image.
getText()Gets a TextOptions object which is used for configuration of text rendering.

For further information on how to customize the conversion process with ImageSaveOptions you can refer to the Fine-Tuning Converters article.

Convert MHTML to JPG in Java using ImageSaveOptions

Aspose.HTML for Java allows converting MHTML to JPG using default or custom save options. Setting different options of ImageSaveOptions class gives you control over the page size, margins, background color, and resolution settings for the output image. These are the steps to convert an MHTML file to JPG format with ImageSaveOptions specifying:

  1. Open an existing MHTML file. Use the openRead() method of System.IO.FileStream class to open and read source files from the file system at the specified path.
  2. Create a new ImageSaveOptions object with JPG ImageFormat and specify the required save options:
    • Use the getBackgroundColor() method to set the background color of the image to Beige.
    • Use the setAnyPage() method to set the size of the image.
  3. Use the convertMHTML(stream, options, savePath) method to save MHTML file as a JPG image. The method takes the stream, options, output file path savePath and performs the conversion operation.

The following Java example shows how to use ImageSaveOptions and create a JPG file with custom page-size, margins, resolution, rendering quality, and background color:

 1    // Open an existing MHTML file for reading
 2    final  FileStream stream = File.openRead(StringExtensions.concat(getDataDir(),  "sample.mht"));
 3    try {        }
 4    finally { if (stream != null) ((IDisposable)stream).dispose(); }
 5
 6    // Prepare a path to save the converted file 
 7    String savePath = Path.combine(getOutputDir(), "sample-options.jpg");
 8
 9    // Initailize the ImageSaveOptions with a custom page size and a background color 
10    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
11    {,
12        PageSetup =
13            {
14                AnyPage = new Page()
15                {
16                    Size = new com.aspose.html.drawing.Size(Length.FromPixels(1000), Length.FromPixels(500))
17                }
18            }
19        BackgroundColor = com.aspose.ms.System.Drawing.Color.getBeige()
20    };
21
22    // Call the convertMHTML() method to convert MHTML to JPG
23    com.aspose.html.converters.Converter.convertMHTML(stream, options, savePath);

Aspose.HTML offers a free online MHTML to JPG Converter that converts MHTML to JPG with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Text “Banner MHTML to JPG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.