SvgImage

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

public final class SvgImage extends VectorImage

Manipulate Scalar Vector Graphics (SVG) image files with our API, utilizing the power of XML-based text format for seamless customization and scalability. Easily load SVG images, rasterize vector elements, and convert to other formats, while controlling compression levels to optimize file size and quality for your projects.

Constructors

ConstructorDescription
SvgImage(String path)Instantiates a new object of the SvgImage class, utilizing the specified path to locate and load the image.
SvgImage(InputStream stream)Creates a new instance of the SvgImage class, loading the image from the provided stream.
SvgImage(int width, int height)Instantiates a new SvgImage object with the specified width and height.
SvgImage(SvgOptions svgOptions, int width, int height)Creates a new instance of the SvgImage class with specified SVG options, image width, and height parameters.

Methods

MethodDescription
isCached()Retrieves a boolean value indicating whether the object’s data is presently cached, eliminating the need for additional data reading operations.
getBitsPerPixel()Retrieves the bits per pixel count of the image.
getFileFormat()Retrieves the file format of the image, providing essential metadata for processing and compatibility checks.
getDefaultOptions(Object[] args)Retrieve the default options configured for the image, providing a baseline setting for various operations such as resizing, compression, or encoding.
cacheData()Cache the data and guarantee that there will be no further loading of data from the underlying DataStreamSupporter.DataStreamContainer(DataStreamSupporter.getDataStreamContainer).
resize(int newWidth, int newHeight, int resizeType)Resize the image to fit the specified dimensions while preserving its aspect ratio.
crop(Rectangle rectangle)Crops the specified rectangle.
rotate(float angle)Rotate image around the center.
setPalette(IColorPalette palette, boolean updateColors)Applies a specified palette to the image, enabling customization of color schemes for aesthetic or functional purposes.

Example: This example shows how to load an SVG image from a file stream and rasterize it to PNG.

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

// Load an SVG image from a file stream.
java.io.InputStream stream = new java.io.FileInputStream(dir + "test.svg");
com.aspose.imaging.fileformats.svg.SvgImage svgImage = new com.aspose.imaging.fileformats.svg.SvgImage(stream);
try {
    // In order to rasterize SVG we need to specify rasterization options.
    com.aspose.imaging.imageoptions.SvgRasterizationOptions rasterizationOptions = new com.aspose.imaging.imageoptions.SvgRasterizationOptions();
    com.aspose.imaging.imageoptions.PngOptions saveOptions = new com.aspose.imaging.imageoptions.PngOptions();
    saveOptions.setVectorRasterizationOptions(rasterizationOptions);

    svgImage.save(dir + "test.output.png", saveOptions);
} finally {
    svgImage.dispose();
    stream.close();
}

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 svgz images to svg format

String file = "example.svgz";
String baseFolder = "D:\\Compressed\\";
String inputFile = baseFolder + file;
String outFile = inputFile + ".svg";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputFile))
{
    com.aspose.imaging.imageoptions.VectorRasterizationOptions vectorRasterizationOptions = new com.aspose.imaging.imageoptions.SvgRasterizationOptions();
    vectorRasterizationOptions.setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()));
    com.aspose.imaging.imageoptions.SvgOptions options = new com.aspose.imaging.imageoptions.SvgOptions();
    options.setVectorRasterizationOptions(vectorRasterizationOptions);
    image.save(outFile, options);
}

Example: The following example shows how to convert a svg images to svgz format

String file = "juanmontoya_lingerie.svg";
String baseFolder = "D:\\Compressed\\";
String inputFile = baseFolder + file;
String outFile = inputFile + ".svgz";
try (com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputFile))
{
    com.aspose.imaging.imageoptions.VectorRasterizationOptions vectorRasterizationOptions = new com.aspose.imaging.imageoptions.SvgRasterizationOptions();
    vectorRasterizationOptions.setPageSize(com.aspose.imaging.Size.to_SizeF(image.getSize()));
    com.aspose.imaging.imageoptions.SvgOptions options = new com.aspose.imaging.imageoptions.SvgOptions();
    options.setVectorRasterizationOptions(vectorRasterizationOptions);
    options.setCompress(true);
    image.save(outFile, options);
}

SvgImage(String path)

public SvgImage(String path)

Instantiates a new object of the SvgImage class, utilizing the specified path to locate and load the image. This constructor facilitates the creation of SVG image instances from external files, enabling seamless integration into software systems and workflows.

Parameters:

ParameterTypeDescription
pathjava.lang.StringThe path to load image from and initialize pixel and palette data with.

SvgImage(InputStream stream)

public SvgImage(InputStream stream)

Creates a new instance of the SvgImage class, loading the image from the provided stream. This constructor enables the direct loading of SVG images from streams, enhancing flexibility and efficiency in handling image resources within software applications.

Parameters:

ParameterTypeDescription
streamjava.io.InputStreamThe stream to load image from and initialize pixel and palette data with.

SvgImage(int width, int height)

public SvgImage(int width, int height)

Instantiates a new SvgImage object with the specified width and height. This constructor allows developers to create SVG images with predefined dimensions, facilitating precise control over the image’s size during initialization.

Parameters:

ParameterTypeDescription
widthintThe image width.
heightintThe image height.

SvgImage(SvgOptions svgOptions, int width, int height)

public SvgImage(SvgOptions svgOptions, int width, int height)

Creates a new instance of the SvgImage class with specified SVG options, image width, and height parameters. This constructor enables developers to initialize SVG images with custom options and dimensions, providing flexibility in managing SVG content and layout.

Parameters:

ParameterTypeDescription
svgOptionsSvgOptionsThe SVG options.
widthintImage width.
heightintImage height.

isCached()

public boolean isCached()

Retrieves a boolean value indicating whether the object’s data is presently cached, eliminating the need for additional data reading operations. This property provides insight into the current caching status, optimizing data retrieval and processing workflows for enhanced performance and efficiency.

Returns: boolean - true if object’s data is cached; otherwise, false.

getBitsPerPixel()

public int getBitsPerPixel()

Retrieves the bits per pixel count of the image. It’s important to note that this parameter is not applicable to vector images, as they are not measured in pixels. This property provides crucial information about the image’s color depth, aiding in processing and manipulation tasks.

Returns: int - The image bits per pixel count.

getFileFormat()

public long getFileFormat()

Retrieves the file format of the image, providing essential metadata for processing and compatibility checks. This property is instrumental in determining the appropriate decoding and encoding strategies for handling the image data effectively across different systems and applications.

Returns: long - file format

getDefaultOptions(Object[] args)

public ImageOptionsBase getDefaultOptions(Object[] args)

Retrieve the default options configured for the image, providing a baseline setting for various operations such as resizing, compression, or encoding. This method is pivotal in ensuring consistent behavior and quality standards across image processing tasks without the need for explicit parameterization.

Parameters:

ParameterTypeDescription
argsjava.lang.Object[]The arguments.

Returns: ImageOptionsBase - Default options

cacheData()

public void cacheData()

Cache the data and guarantee that there will be no further loading of data from the underlying DataStreamSupporter.DataStreamContainer(DataStreamSupporter.getDataStreamContainer). This optimization enhances performance by eliminating redundant data retrieval operations, especially beneficial in scenarios requiring frequent access to the image data.

resize(int newWidth, int newHeight, int resizeType)

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

Resize the image to fit the specified dimensions while preserving its aspect ratio. This method provides a convenient way to adjust the size of the image without distorting its proportions, ensuring optimal display or storage according to the desired dimensions.

Parameters:

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

crop(Rectangle rectangle)

public void crop(Rectangle rectangle)

Crops the specified rectangle.

Parameters:

ParameterTypeDescription
rectangleRectangleThe rectangle.

rotate(float angle)

public void rotate(float angle)

Rotate image around the center.

Parameters:

ParameterTypeDescription
anglefloatThe rotate angle in degrees. Positive values will rotate clockwise.

setPalette(IColorPalette palette, boolean updateColors)

public void setPalette(IColorPalette palette, boolean updateColors)

Applies a specified palette to the image, enabling customization of color schemes for aesthetic or functional purposes. This method provides flexibility in managing color palettes to suit various design or application requirements.

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.