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
Constructor | Description |
---|---|
SvgImage(String path) | Initializes a new instance of the SvgImage class. |
SvgImage(InputStream stream) | Initializes a new instance of the SvgImage class. |
SvgImage(int width, int height) | Initializes a new instance of the SvgImage class. |
SvgImage(SvgOptions svgOptions, int width, int height) | Initializes a new instance of the SvgImage class. |
Methods
Method | Description |
---|---|
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 this parameter is not applicable to vector images |
getHeight() | Gets the image height. |
getWidth() | Gets the image width. |
getFileFormat() | Gets a value of file format |
getDefaultOptions(Object[] args) | Gets the default options. |
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. |
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)
Initializes a new instance of the SvgImage
class.
Parameters:
Parameter | Type | Description |
---|---|---|
path | java.lang.String | The path to load image from and initialize pixel and palette data with. |
SvgImage(InputStream stream)
public SvgImage(InputStream stream)
Initializes a new instance of the SvgImage
class.
Parameters:
Parameter | Type | Description |
---|---|---|
stream | java.io.InputStream | The stream to load image from and initialize pixel and palette data with. |
SvgImage(int width, int height)
public SvgImage(int width, int height)
Initializes a new instance of the SvgImage
class.
Parameters:
Parameter | Type | Description |
---|---|---|
width | int | The image width. |
height | int | The image height. |
SvgImage(SvgOptions svgOptions, int width, int height)
public SvgImage(SvgOptions svgOptions, int width, int height)
Initializes a new instance of the SvgImage
class.
Parameters:
Parameter | Type | Description |
---|---|---|
svgOptions | SvgOptions | The SVG options. |
width | int | Image width. |
height | int | Image height. |
isCached()
public boolean isCached()
Gets a value indicating whether object’s data is cached currently and no data reading is required.
Returns:
boolean - true
if object’s data is cached; otherwise, false
.
getBitsPerPixel()
public int getBitsPerPixel()
Gets the image bits per pixel count this parameter is not applicable to vector images
Returns: int - The image bits per pixel count.
getHeight()
public int getHeight()
Gets the image height.
Returns: int - The image height.
getWidth()
public int getWidth()
Gets the image width.
Returns: int - The image width.
getFileFormat()
public long getFileFormat()
Gets a value of file format
Returns: long - file format
getDefaultOptions(Object[] args)
public ImageOptionsBase getDefaultOptions(Object[] args)
Gets the default options.
Parameters:
Parameter | Type | Description |
---|---|---|
args | java.lang.Object[] | The arguments. |
Returns: ImageOptionsBase - Default options
cacheData()
public void 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)
public void resize(int newWidth, int newHeight, int resizeType)
Resizes the image.
Parameters:
Parameter | Type | Description |
---|---|---|
newWidth | int | The new width. |
newHeight | int | The new height. |
resizeType | int | The resize type. |
resize(int newWidth, int newHeight, ImageResizeSettings settings)
public void resize(int newWidth, int newHeight, ImageResizeSettings settings)
Resizes the image.
Parameters:
Parameter | Type | Description |
---|---|---|
newWidth | int | The new width. |
newHeight | int | The new height. |
settings | ImageResizeSettings | The resize settings. |
rotateFlip(int rotateFlipType)
public void rotateFlip(int rotateFlipType)
Rotates, flips, or rotates and flips the image.
Parameters:
Parameter | Type | Description |
---|---|---|
rotateFlipType | int | Type of the rotating flip. |
setPalette(IColorPalette palette, boolean updateColors)
public void setPalette(IColorPalette palette, boolean updateColors)
Sets the image palette.
Parameters:
Parameter | Type | Description |
---|---|---|
palette | IColorPalette | The palette to set. |
updateColors | boolean | 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. |