public final class SvgImage extends VectorImage implements IHasXmpData
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.
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();
}
| Constructor and Description |
|---|
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(String path)
Instantiates a new object of the
SvgImage class, utilizing the
specified path to locate and load the image. |
SvgImage(SvgOptions svgOptions,
int width,
int height)
Creates a new instance of the
SvgImage class with specified SVG
options, image width, and height parameters. |
| Modifier and Type | Method and Description |
|---|---|
void |
cacheData()
Cache the data and guarantee that there will be no further loading of data from
the underlying
DataStreamSupporter.DataStreamContainer(DataStreamSupporter.getDataStreamContainer()). |
void |
crop(Rectangle rectangle)
Crops the specified rectangle.
|
int |
getBitsPerPixel()
Retrieves the bits per pixel count of the image.
|
long |
getFileFormat()
Retrieves the file format of the image, providing essential metadata for
processing and compatibility checks.
|
XmpPacketWrapper |
getXmpData()
Gets XMP data.
|
boolean |
isCached()
Retrieves a boolean value indicating whether the object's data is presently
cached, eliminating the need for additional data reading operations.
|
void |
resize(int newWidth,
int newHeight,
int resizeType)
Resize the image to fit the specified dimensions while preserving its aspect
ratio.
|
void |
rotate(float angle)
Rotate image around the center.
|
void |
setPalette(IColorPalette palette,
boolean updateColors)
Applies a specified palette to the image, enabling customization of color
schemes for aesthetic or functional purposes.
|
void |
setXmpData(XmpPacketWrapper value)
Sets XMP data.
|
getDefaultOptions, getEmbeddedImages, getHeight, getHeightF, getSizeF, getWidth, getWidthF, removeBackground, removeBackground, resize, rotateFlipcanLoad, canLoad, canLoad, canLoad, canSave, create, create, create, create, create, create, crop, getBackgroundColor, getBounds, getBufferSizeHint, getContainer, getFileFormat, getFileFormat, getFittingRectangle, getFittingRectangle, getInterruptMonitor, getIProgressEventHandler, getOriginalOptions, getPalette, getProgressEventHandlerInfo, getProportionalHeight, getProportionalWidth, getSerializedStream, getSize, hasBackgroundColor, isAutoAdjustPalette, isUsePalette, load, load, load, load, load, load, removeMetadata, resize, resizeHeightProportionally, resizeHeightProportionally, resizeHeightProportionally, resizeWidthProportionally, resizeWidthProportionally, resizeWidthProportionally, save, save, save, save, save, save, save, save, setAutoAdjustPalette, setBackgroundColor, setBackgroundColor, setBufferSizeHint, setInterruptMonitor, setPalettegetDataStreamContainer, save, save, saveclose, dispose, getDisposedpublic 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.
path - The path to load image from and initialize pixel and palette data with.com.aspose.ms.System.ArgumentNullException - path is null.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.
stream - The stream to load image from and initialize pixel and palette data with.com.aspose.ms.System.ArgumentNullException - stream is null.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();
}
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.
width - The image width.height - The image 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.
svgOptions - The SVG options.width - Image width.height - Image height.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.
isCached in class DataStreamSupportertrue if object's data is cached; otherwise, false.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.
getBitsPerPixel in class Imagecom.aspose.ms.System.NotImplementedException - Invalid for vector imagespublic 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.
getFileFormat in class ImageFileFormatpublic XmpPacketWrapper getXmpData()
Gets XMP data.
getXmpData in interface IHasXmpDatapublic void setXmpData(XmpPacketWrapper value)
Sets XMP data.
setXmpData in interface IHasXmpDatavalue - XMP data.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.
cacheData in class DataStreamSupporterpublic 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.
resize in class VectorImagenewWidth - The new width.newHeight - The new height.resizeType - The resize type.com.aspose.ms.System.NotImplementedException - this feature is not implementedResizeTypepublic void crop(Rectangle rectangle)
Crops the specified rectangle.
crop in class VectorImagerectangle - The rectangle.public void rotate(float angle)
Rotate image around the center.
rotate in class VectorImageangle - The rotate angle in degrees. Positive values will rotate clockwise.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.
setPalette in class Imagepalette - The palette to set.updateColors - if 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.com.aspose.ms.System.NotImplementedException - if not implementedCopyright (c) 2008-2025 Aspose Pty Ltd. All Rights Reserved.