EmfImage

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 final class EmfImage extends MetaImage

The API for Enhanced Metafile Format (EMF) vector image format support is a comprehensive tool for processing graphical images in a device-independent manner while preserving their original properties. Developed to maintain proportions, dimensions, colors, and other graphic attributes, it includes EMF Plus format support and features for cropping regions, resizing canvas and images, rotating, flipping, setting image palettes, exporting and importing to APS device context, compressing and converting EMF to other formats, ensuring versatile manipulation and seamless integration of EMF images across applications.

Constructors

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

Methods

MethodDescription
getHeader()Gets or sets the header record
setHeader(EmfMetafileHeader value)Gets or sets the header record
isCached()Gets a value indicating whether object’s data is cached currently and no data reading is required.
getRecords()Gets or sets the records.
setRecords(MetaObjectList value)Gets or sets the records.
getFileFormat()Gets a value of file format
getBitsPerPixel()Gets the image bits per pixel count this parameter is not applicable to vector images
getWidth()Gets the image width.
getHeight()Gets the image height.
cacheData()Caches the data and ensures no additional data loading will be performed from the underlying DataStreamSupporter.DataStreamContainer.
getUsedFonts()Returns the list of font which used inside metafile.
crop(Rectangle rectangle)Crops the specified rectangle.
resizeCanvas(Rectangle newRectangle)Resizes the canvas.
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.
getDefaultOptions(Object[] args)Gets the default options.

Example: This example shows how to load a EMF image from a file and convert it to SVG using EmfRasterizationOptions.

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

// Using Aspose.Imaging.Image.Load is a unified way to load all types of images including EMF.
com.aspose.imaging.fileformats.emf.EmfImage emfImage = (com.aspose.imaging.fileformats.emf.EmfImage) com.aspose.imaging.Image.load(dir + "test.emf");
try {
    com.aspose.imaging.imageoptions.SvgOptions saveOptions = new com.aspose.imaging.imageoptions.SvgOptions();

    // Text will be converted to shapes.
    saveOptions.setTextAsShapes(true);

    com.aspose.imaging.imageoptions.EmfRasterizationOptions rasterizationOptions = new com.aspose.imaging.imageoptions.EmfRasterizationOptions();

    // The background color of the drawing surface.
    rasterizationOptions.setBackgroundColor(com.aspose.imaging.Color.getWhiteSmoke());

    // The page size.
    rasterizationOptions.setPageSize(new com.aspose.imaging.SizeF(emfImage.getWidth(), emfImage.getHeight()));

    // If embedded emf exists, then render emf; otherwise render wmf.
    rasterizationOptions.setRenderMode(com.aspose.imaging.fileformats.emf.EmfRenderMode.Auto);

    // Set the horizontal margin
    rasterizationOptions.setBorderX(50);

    // Set the vertical margin
    rasterizationOptions.setBorderY(50);

    saveOptions.setVectorRasterizationOptions(rasterizationOptions);

    emfImage.save(dir + "test.output.svg", saveOptions);
} finally {
    emfImage.dispose();
}

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 emz images to emf format

String file = "example.emz";
String baseFolder = "D:\\Compressed\\";
String inputFile = (baseFolder + file);
String outFile = inputFile + ".emf";
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.EmfRasterizationOptions()
    {{
        setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()));
    }};
    image.save(outFile, new com.aspose.imaging.imageoptions.EmfOptions()
    {{
        setVectorRasterizationOptions(vectorRasterizationOptions);
    }});
}

Example: The following example shows how to convert a emf images to emz format

String file = "input.emf";
String baseFolder = "D:\\Compressed\\";
String inputFile = baseFolder + file;
String outFile = inputFile + ".emz";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputFile))
{
    com.aspose.imaging.imageoptions.VectorRasterizationOptions vectorRasterizationOptions = new com.aspose.imaging.imageoptions.EmfRasterizationOptions();
    vectorRasterizationOptions.setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()));
    com.aspose.imaging.imageoptions.EmfOptions options = new com.aspose.imaging.imageoptions.EmfOptions();
    options.setVectorRasterizationOptions(vectorRasterizationOptions);
    options.setCompress(true);
    image.save(outFile, options);
}

EmfImage()

public EmfImage()

Initializes a new instance of the EmfImage class.

EmfImage(int width, int height)

public EmfImage(int width, int height)

Initializes a new instance of the EmfImage class.

Parameters:

ParameterTypeDescription
widthintThe width.
heightintThe height.

getHeader()

public EmfMetafileHeader getHeader()

Gets or sets the header record

Returns: EmfMetafileHeader

setHeader(EmfMetafileHeader value)

public void setHeader(EmfMetafileHeader value)

Gets or sets the header record

Parameters:

ParameterTypeDescription
valueEmfMetafileHeader

isCached()

public boolean isCached()

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

Value: true if object’s data is cached; otherwise, false.

Returns: boolean

getRecords()

public MetaObjectList getRecords()

Gets or sets the records.

Value: The records.

Returns: MetaObjectList

setRecords(MetaObjectList value)

public void setRecords(MetaObjectList value)

Gets or sets the records.

Value: The records.

Parameters:

ParameterTypeDescription
valueMetaObjectList

getFileFormat()

public long getFileFormat()

Gets a value of file format

Returns: long

getBitsPerPixel()

public int getBitsPerPixel()

Gets the image bits per pixel count this parameter is not applicable to vector images

Returns: int

getWidth()

public int getWidth()

Gets the image width.

Value: The image width.

Returns: int

getHeight()

public int getHeight()

Gets the image height.

Value: The image height.

Returns: int

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 EMF 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.emf.EmfImage emfImage = (com.aspose.imaging.fileformats.emf.EmfImage) com.aspose.imaging.Image.load(dir + "test.emf");
try {
    // Cache data to load all records.
    emfImage.cacheData();
    System.out.println("The total number of records: " + emfImage.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 : emfImage.getRecords()) {
        com.aspose.imaging.fileformats.emf.emf.records.EmfRecord record = (com.aspose.imaging.fileformats.emf.emf.records.EmfRecord) obj;

        Class objType = record.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 {
    emfImage.dispose();
}

//The output may look like this:
//The total number of records: 1188
//Record Type                              Count
//----------------------------------------------
//EmfMetafileHeader:                       1
//EmfSetBkMode:                            1
//EmfSetTextAlign:                         1
//EmfSetRop2:                              1
//EmfSetWorldTransform:                    1
//EmfExtSelectClipRgn:                     1
//EmfCreateBrushIndirect:                  113
//EmfSelectObject:                         240
//EmfCreatePen:                            116
//EmfSetPolyFillMode:                      1
//EmfBeginPath:                            120
//EmfMoveToEx:                             122
//EmfPolyBezierTo16:                       36
//EmfLineTo:                               172
//EmfCloseFigure:                          14
//EmfEndPath:                              120
//EmfStrokeAndFillPath:                    113
//EmfStrokePath:                           7
//EmfSetTextColor:                         2
//EmfExtCreateFontIndirectW:               2
//EmfExtTextOutW:                          2
//EmfStretchBlt:                           1
//EmfEof:                                  1

getUsedFonts()

public String[] getUsedFonts()

Returns the list of font which used inside metafile.

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

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.

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.

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 rotating flip.

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.

getDefaultOptions(Object[] args)

public ImageOptionsBase getDefaultOptions(Object[] args)

Gets the default options.

Parameters:

ParameterTypeDescription
argsjava.lang.Object[]The arguments.

Returns: ImageOptionsBase - Default options