Class SvgImage

SvgImage class

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.

public sealed class SvgImage : VectorImage

Constructors

NameDescription
SvgImage(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.
SvgImage(string)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.
SvgImage(int, int)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.
SvgImage(SvgOptions, int, int)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.

Properties

NameDescription
AutoAdjustPalette { get; set; }Gets or sets a value indicating whether automatic adjust palette.
virtual BackgroundColor { get; set; }Gets or sets a value for the background color.
override BitsPerPixel { get; }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.
Bounds { get; }Gets the image bounds.
BufferSizeHint { get; set; }Gets or sets the buffer size hint which is defined max allowed size for all internal buffers.
Container { get; }Gets the Image container.
DataStreamContainer { get; }Gets the object’s data stream.
Disposed { get; }Gets a value indicating whether this instance is disposed.
override FileFormat { get; }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.
virtual HasBackgroundColor { get; set; }Gets or sets a value indicating whether image has background color.
override Height { get; }Retrieves the height of the image, indicating its vertical size in pixels. This property provides essential information for layout calculations and rendering processes, ensuring accurate display and manipulation of the image.
virtual HeightF { get; }Gets the object height, in inches.
InterruptMonitor { get; set; }Gets or sets the interrupt monitor.
override IsCached { get; }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.
Palette { get; set; }Gets or sets the color palette. The color palette is not used when pixels are represented directly.
Size { get; }Gets the image size.
SizeF { get; }Gets the object size, in inches.
virtual UsePalette { get; }Gets a value indicating whether the image palette is used.
override Width { get; }Returns the width of the image, denoting its horizontal size in pixels. This property is crucial for layout computations and display rendering, ensuring precise positioning and visualization of the image content.
virtual WidthF { get; }Gets the object width, in inches.

Methods

NameDescription
override CacheData()Cache the data and guarantee that there will be no further loading of data from the underlying DataStreamContainer. This optimization enhances performance by eliminating redundant data retrieval operations, especially beneficial in scenarios requiring frequent access to the image data.
CanSave(ImageOptionsBase)Determines whether image can be saved to the specified file format represented by the passed save options.
Dispose()Disposes the current instance.
override GetDefaultOptions(object[])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.
virtual GetEmbeddedImages()Gets the embedded images.
virtual GetOriginalOptions()Gets the options based on the original file settings. This can be helpful to keep bit-depth and other parameters of the original image unchanged. For example, if we load a black-white PNG image with 1 bit per pixel and then save it using the Save method, the output PNG image with 8-bit per pixel will be produced. To avoid it and save PNG image with 1-bit per pixel, use this method to get corresponding saving options and pass them to the Save method as the second parameter.
virtual GetSerializedStream(ImageOptionsBase, Rectangle, out int)Converts to aps.
RemoveBackground()Removes the background.
RemoveBackground(RemoveBackgroundSettings)Removes the background.
virtual RemoveMetadata()Removes metadata.
Resize(int, int)Resizes the image. The default NearestNeighbourResample is used.
override Resize(int, int, ImageResizeSettings)Adjusts the image size according to the specified settings, enabling customization of the resizing process to meet specific requirements. This method offers flexibility in resizing operations, allowing for precise control over aspects such as interpolation methods, aspect ratio preservation, and output quality.
override Resize(int, 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.
ResizeHeightProportionally(int)Resizes the height proportionally. The default NearestNeighbourResample is used.
virtual ResizeHeightProportionally(int, ImageResizeSettings)Resizes the height proportionally.
virtual ResizeHeightProportionally(int, ResizeType)Resizes the height proportionally.
ResizeWidthProportionally(int)Resizes the width proportionally. The default NearestNeighbourResample is used.
virtual ResizeWidthProportionally(int, ImageResizeSettings)Resizes the width proportionally.
virtual ResizeWidthProportionally(int, ResizeType)Resizes the width proportionally.
override RotateFlip(RotateFlipType)Applies rotation, flipping, or both to the image as per the specified parameters. This method offers versatility in image manipulation, allowing for seamless adjustment of orientation to achieve desired visual effects or correct orientation errors.
Save()Saves the image data to the underlying stream.
Save(Stream)Saves the object’s data to the specified stream.
override Save(string)Saves the image to the specified file location.
Save(Stream, ImageOptionsBase)Saves the image’s data to the specified stream in the specified file format according to save options.
virtual Save(string, bool)Saves the object’s data to the specified file location.
virtual Save(string, ImageOptionsBase)Saves the object’s data to the specified file location in the specified file format according to save options.
virtual Save(Stream, ImageOptionsBase, Rectangle)Saves the image’s data to the specified stream in the specified file format according to save options.
virtual Save(string, ImageOptionsBase, Rectangle)Saves the object’s data to the specified file location in the specified file format according to save options.
override SetPalette(IColorPalette, bool)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.

Examples

The following example shows how to convert a svgz images to svg fromat

[C#]

string file = "example.svgz";
string baseFolder = System.IO.Path.Combine("D:", "Compressed");
string inputFile = System.IO.Path.Combine(baseFolder, file);
string outFile = inputFile + ".svg";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(inputFile))
{
    Aspose.Imaging.ImageOptions.VectorRasterizationOptions vectorRasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions() { PageSize = image.Size};
    image.Save(outFile, new Aspose.Imaging.ImageOptions.SvgOptions() {VectorRasterizationOptions = vectorRasterizationOptions});
}

The following example shows how to convert a svg images to svgz fromat

[C#]

string file = "juanmontoya_lingerie.svg";
string baseFolder = System.IO.Path.Combine("D:", "Compressed");
string inputFile = System.IO.Path.Combine(baseFolder, file);
string outFile = inputFile + ".svgz";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(inputFile))
{
    Aspose.Imaging.ImageOptions.VectorRasterizationOptions vectorRasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions() { PageSize = image.Size};
    image.Save(outFile, new Aspose.Imaging.ImageOptions.SvgOptions() {VectorRasterizationOptions = vectorRasterizationOptions, Compress = true});
}

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

[C#]

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

// Load an SVG image from a file stream.
using (System.IO.Stream stream = System.IO.File.OpenRead(dir + "test.svg"))
using (Aspose.Imaging.FileFormats.Svg.SvgImage svgImage = new Aspose.Imaging.FileFormats.Svg.SvgImage(stream))
{
    // In order to rasterize SVG we need to specify rasterization options.
    Aspose.Imaging.ImageOptions.SvgRasterizationOptions rasterizationOptions = new Aspose.Imaging.ImageOptions.SvgRasterizationOptions();
    Aspose.Imaging.ImageOptions.PngOptions saveOptions = new Aspose.Imaging.ImageOptions.PngOptions();
    saveOptions.VectorRasterizationOptions = rasterizationOptions;

    svgImage.Save(dir + "test.output.png", saveOptions);
}

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

[C#]

string[] files = new[] {"example.emz", "example.wmz", "example.svgz"};
string baseFolder = System.IO.Path.Combine("D:","Compressed");
foreach (var file in files)
{
    string inputFile = System.IO.Path.Combine(baseFolder, file);
    string outFile = inputFile + ".png";
    using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(inputFile))
    {
        Aspose.Imaging.ImageOptions.VectorRasterizationOptions vectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)image.GetDefaultOptions(new object[] { Color.White, image.Width, image.Height });
        image.Save(outFile, new Aspose.Imaging.ImageOptions.PngOptions(){VectorRasterizationOptions = vectorRasterizationOptions});
    }
}

See Also