WmfImage

Inheritance: java.lang.Object, com.aspose.imaging.DisposableObject, com.aspose.imaging.DataStreamSupporter, com.aspose.imaging.Image, com.aspose.imaging.VectorImage, com.aspose.imaging.fileformats.emf.MetaImage

public class WmfImage extends MetaImage

Manipulate Microsoft Windows Metafile (WMF) images with our API, seamlessly handling both vector and bitmap data stored within variable-length records. Resize, rotate, and flip images with ease while setting custom image palettes. Convert WMF files to compressed WMZ formats or save them in raster image formats for versatile usage across platforms and applications.

Constructors

ConstructorDescription
WmfImage()Initializes a new instance of the WmfImage class.
WmfImage(int width, int height)Initializes a new instance of the WmfImage class.

Methods

MethodDescription
isCached()Gets a value indicating whether object’s data is cached currently and no data reading is required.
getBitsPerPixel()Gets the image bits per pixel count.
getWidth()Gets the image width.
getHeight()Gets the image height.
getInch()Gets or sets the inch.
setInch(int value)Gets or sets the inch.
getFileFormat()Gets a value of file format
getFrameBounds()Gets the frame bounds.
cacheData()Caches the data and ensures no additional data loading will be performed from the underlying DataStreamSupporter.DataStreamContainer.
resize(int newWidth, int newHeight, int resizeType)Resizes the image.
resize(int newWidth, int newHeight, ImageResizeSettings settings)Resizes the image.
rotateFlip(int rotateFlipType)Rotates, flips, or rotates and flips the image.
setPalette(IColorPalette palette, boolean updateColors)Sets the image palette.
getUsedFonts()Returns the list of font which used inside metafile.
getDefaultOptions(Object[] args)Gets the default options.
crop(Rectangle rectangle)Crops the specified rectangle.
resizeCanvas(Rectangle newRectangle)Resizes the canvas.
addRecord(WmfObject record)Adds the record.
getPostScript()Gets the post script.

Example: The following example shows how to convert a compressed images (*.

The following example shows how to convert a compressed images (.emz,.wmz, *.svgz) to raster format

String[] files = new String[]{ "example.emz", "example.wmz", "example.svgz" };
String baseFolder = "D:\\Compressed\\";
for(String file : files)
{
    String inputFile = (baseFolder + file);
    String outFile = inputFile + ".png";
    try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputFile))
    {
        final com.aspose.imaging.imageoptions.VectorRasterizationOptions vectorRasterizationOptions = 
                (com.aspose.imaging.imageoptions.VectorRasterizationOptions) image.getDefaultOptions(new Object[]{Color.getWhite(), image.getWidth(), image.getHeight()});
        image.save(outFile, new com.aspose.imaging.imageoptions.PngOptions()
        {{
            setVectorRasterizationOptions(vectorRasterizationOptions);
        }});
    }
}

Example: The following example shows how to convert a wmz images to wmf format

String file = "example.wmz";
String baseFolder = "D:\\Compressed\\";
String inputFile = baseFolder + file;
String outFile = inputFile + ".wmf";
try (final com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputFile))
{
    final com.aspose.imaging.imageoptions.VectorRasterizationOptions vectorRasterizationOptions = new com.aspose.imaging.imageoptions.WmfRasterizationOptions()
    {{
        setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()));
    }};
                
    image.save(outFile, new com.aspose.imaging.imageoptions.WmfOptions()
    {{
        setVectorRasterizationOptions(vectorRasterizationOptions);
    }});
}

Example: The following example shows how to convert a wmf images to wmz format

String file = "castle.wmf";
String baseFolder = "D:\\Compressed\\";
String inputFile = baseFolder + file;
String outFile = inputFile + ".wmz";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputFile))
{
    com.aspose.imaging.imageoptions.VectorRasterizationOptions vectorRasterizationOptions = new com.aspose.imaging.imageoptions.WmfRasterizationOptions();
    vectorRasterizationOptions.setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()));
    com.aspose.imaging.imageoptions.WmfOptions options = new com.aspose.imaging.imageoptions.WmfOptions();
    options.setVectorRasterizationOptions(vectorRasterizationOptions);
    options.setCompress(true);
    image.save(outFile, options);
}

WmfImage()

public WmfImage()

Initializes a new instance of the WmfImage class.

WmfImage(int width, int height)

public WmfImage(int width, int height)

Initializes a new instance of the WmfImage class.

Parameters:

ParameterTypeDescription
widthintThe width.
heightintThe height.

isCached()

public boolean isCached()

Gets a value indicating whether object’s data is cached currently and no data reading is required.

Returns: boolean

getBitsPerPixel()

public int getBitsPerPixel()

Gets the image bits per pixel count.

Returns: int

getWidth()

public int getWidth()

Gets the image width.

Returns: int

getHeight()

public int getHeight()

Gets the image height.

Returns: int

getInch()

public int getInch()

Gets or sets the inch.

Value: The inch.

Returns: int

setInch(int value)

public void setInch(int value)

Gets or sets the inch.

Value: The inch.

Parameters:

ParameterTypeDescription
valueint

getFileFormat()

public long getFileFormat()

Gets a value of file format

Returns: long

getFrameBounds()

public final Rectangle getFrameBounds()

Gets the frame bounds.

Value: The frame bounds.

Returns: Rectangle - the frame bounds.

cacheData()

public void cacheData()

Caches the data and ensures no additional data loading will be performed from the underlying DataStreamSupporter.DataStreamContainer.

Example: This example shows how to load a WMF image from a file and list all of its records.

String dir = "c:\\temp\\";

// Using Aspose.Imaging.Image.Load is a unified way to load all types of images including WMF.
com.aspose.imaging.fileformats.wmf.WmfImage wmfImage = (com.aspose.imaging.fileformats.wmf.WmfImage) com.aspose.imaging.Image.load(dir + "test.wmf");
try {
    // Cache data to load all records.
    wmfImage.cacheData();
    System.out.println("The total number of records: " + wmfImage.getRecords().size());

    // The key is a record type, the value is number of records of that type in the WMF image.
    java.util.HashMap<Class, Integer> types = new java.util.HashMap<>();

    // Gather statistics
    for (Object obj : wmfImage.getRecords()) {
        com.aspose.imaging.fileformats.wmf.objects.WmfObject wmfObj = (com.aspose.imaging.fileformats.wmf.objects.WmfObject) obj;

        Class objType = wmfObj.getClass();
        if (!types.containsKey(objType)) {
            types.put(objType, 1);
        } else {
            int n = types.get(objType);
            types.put(objType, n + 1);
        }
    }

    // Print statistics
    System.out.println("Record Type                              Count");
    System.out.println("----------------------------------------------");
    for (java.util.Map.Entry<Class, Integer> entry : types.entrySet()) {
        String objectType = entry.getKey().getSimpleName();
        int numberOfEntrances = entry.getValue();

        // Align output with spaces
        int alignmentPos = 40;
        char[] chars = new char[alignmentPos - objectType.length()];
        java.util.Arrays.fill(chars, ' ');
        String gap = new String(chars);

        System.out.println(objectType + ":" + gap + numberOfEntrances);
    }
} finally {
    wmfImage.dispose();
}

//The output may look like this:
//The total number of records: 613
//Record Type                              Count
//----------------------------------------------
//WmfSetBkMode:                            1
//WmfSetTextAlign:                         1
//WmfSetRop2:                              1
//WmfSetWindowOrg:                         1
//WmfSetWindowExt:                         1
//WmfCreateBrushInDirect:                  119
//WmfSelectObject:                         240
//WmfCreatePenInDirect:                    119
//WmfSetPolyFillMode:                      1
//WmfPolyPolygon:                          114
//WmfPolyLine:                             7
//WmfSetTextColor:                         2
//WmfCreateFontInDirect:                   2
//WmfExtTextOut:                           2
//WmfDibStrechBlt:                         1
//WmfEof:                                  1

resize(int newWidth, int newHeight, int resizeType)

public void resize(int newWidth, int newHeight, int resizeType)

Resizes the image.

Parameters:

ParameterTypeDescription
newWidthintThe new width.
newHeightintThe new height.
resizeTypeintThe resize type.

Example: This example loads a WMF image and resizes it using various resizing methods.

String dir = "c:\\temp\\";

com.aspose.imaging.fileformats.wmf.WmfImage image = (com.aspose.imaging.fileformats.wmf.WmfImage) com.aspose.imaging.Image.load(dir + "sample.wmf");
try {
    // Scale up by 2 times using Nearest Neighbour resampling.
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
} finally {
    image.dispose();
}

image = (com.aspose.imaging.fileformats.wmf.WmfImage) com.aspose.imaging.Image.load(dir + "sample.wmf");
try {
    // Scale down by 2 times using Nearest Neighbour resampling.
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
} finally {
    image.dispose();
}

image = (com.aspose.imaging.fileformats.wmf.WmfImage) com.aspose.imaging.Image.load(dir + "sample.wmf");
try {
    // Scale up by 2 times using Bilinear resampling.
    image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);
} finally {
    image.dispose();
}

image = (com.aspose.imaging.fileformats.wmf.WmfImage) com.aspose.imaging.Image.load(dir + "sample.wmf");
try {
    // Scale down by 2 times using Bilinear resampling.
    image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);
} finally {
    image.dispose();
}

resize(int newWidth, int newHeight, ImageResizeSettings settings)

public void resize(int newWidth, int newHeight, ImageResizeSettings settings)

Resizes the image.

Parameters:

ParameterTypeDescription
newWidthintThe new width.
newHeightintThe new height.
settingsImageResizeSettingsThe resize settings.

rotateFlip(int rotateFlipType)

public void rotateFlip(int rotateFlipType)

Rotates, flips, or rotates and flips the image.

Parameters:

ParameterTypeDescription
rotateFlipTypeintType of the rotation and flipping.

setPalette(IColorPalette palette, boolean updateColors)

public void setPalette(IColorPalette palette, boolean updateColors)

Sets the image palette.

Parameters:

ParameterTypeDescription
paletteIColorPaletteThe palette to set.
updateColorsbooleanif set to true colors will be updated according to the new palette; otherwise color indexes remain unchanged. Note that unchanged indexes may crash the image on loading if some indexes have no corresponding palette entries.

getUsedFonts()

public String[] getUsedFonts()

Returns the list of font which used inside metafile.

Returns: java.lang.String[] - The font list

getDefaultOptions(Object[] args)

public ImageOptionsBase getDefaultOptions(Object[] args)

Gets the default options.

Parameters:

ParameterTypeDescription
argsjava.lang.Object[]The arguments.

Returns: ImageOptionsBase - Default options

crop(Rectangle rectangle)

public void crop(Rectangle rectangle)

Crops the specified rectangle.

Parameters:

ParameterTypeDescription
rectangleRectangleThe rectangle.

resizeCanvas(Rectangle newRectangle)

public void resizeCanvas(Rectangle newRectangle)

Resizes the canvas.

Parameters:

ParameterTypeDescription
newRectangleRectangleThe new rectangle.

addRecord(WmfObject record)

public int addRecord(WmfObject record)

Adds the record.

Parameters:

ParameterTypeDescription
recordWmfObjectThe record.

Returns: int - Number of record.

getPostScript()

public final String getPostScript()

Gets the post script.

Returns: java.lang.String - The post script