Class SvgImage

Class SvgImage

Namespace: Aspose.Imaging.FileFormats.Svg
Assembly: Aspose.Imaging.dll (25.7.0)

Manipulate Scalar Vector Graphics (SVG) image files with our API, utilizingthe 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.

[JsonObject(MemberSerialization.OptIn)]
   public sealed class SvgImage : VectorImage, IDisposable, IObjectWithBounds, IObjectWithSizeF, IHasXmpData, IHasMetadata
   {
   }

Inheritance

object DisposableObject DataStreamSupporter Image VectorImage SvgImage

Implements

IDisposable , IObjectWithBounds , IObjectWithSizeF , IHasXmpData , IHasMetadata

Inherited Members

VectorImage.GetDefaultOptions(object[]) , VectorImage.GetEmbeddedImages() , VectorImage.RemoveBackground() , VectorImage.RemoveBackground(RemoveBackgroundSettings) , VectorImage.Resize(int, int, ResizeType) , VectorImage.Resize(int, int, ImageResizeSettings) , VectorImage.RotateFlip(RotateFlipType) , VectorImage.Crop(Rectangle) , VectorImage.Rotate(float) , VectorImage.SizeF , VectorImage.WidthF , VectorImage.HeightF , VectorImage.Width , VectorImage.Height , Image.CanLoad(string) , Image.CanLoad(string, LoadOptions) , Image.CanLoad(Stream) , Image.CanLoad(Stream, LoadOptions) , Image.Create(ImageOptionsBase, int, int) , Image.Create(Image[]) , Image.Create(MultipageCreateOptions) , Image.Create(string[], bool) , Image.Create(string[]) , Image.Create(Image[], bool) , Image.GetFileFormat(string) , Image.GetFileFormat(Stream) , Image.GetFittingRectangle(Rectangle, int, int) , Image.GetFittingRectangle(Rectangle, int[], int, int) , Image.Load(string, LoadOptions) , Image.Load(string) , Image.Load(Stream, LoadOptions) , Image.Load(Stream) , Image.GetProportionalWidth(int, int, int) , Image.GetProportionalHeight(int, int, int) , Image.RemoveMetadata() , Image.CanSave(ImageOptionsBase) , Image.Resize(int, int) , Image.Resize(int, int, ResizeType) , Image.Resize(int, int, ImageResizeSettings) , Image.GetDefaultOptions(object[]) , Image.GetOriginalOptions() , Image.ResizeWidthProportionally(int) , Image.ResizeHeightProportionally(int) , Image.ResizeWidthProportionally(int, ResizeType) , Image.ResizeHeightProportionally(int, ResizeType) , Image.ResizeWidthProportionally(int, ImageResizeSettings) , Image.ResizeHeightProportionally(int, ImageResizeSettings) , Image.RotateFlip(RotateFlipType) , Image.Rotate(float) , Image.Crop(Rectangle) , Image.Crop(int, int, int, int) , Image.Save() , Image.Save(string) , Image.Save(string, ImageOptionsBase) , Image.Save(string, ImageOptionsBase, Rectangle) , Image.Save(Stream, ImageOptionsBase) , Image.Save(Stream, ImageOptionsBase, Rectangle) , Image.GetSerializedStream(ImageOptionsBase, Rectangle, out int) , Image.SetPalette(IColorPalette, bool) , Image.BitsPerPixel , Image.Bounds , Image.Container , Image.Height , Image.Palette , Image.UsePalette , Image.Size , Image.Width , Image.InterruptMonitor , Image.BufferSizeHint , Image.AutoAdjustPalette , Image.HasBackgroundColor , Image.FileFormat , Image.BackgroundColor , DataStreamSupporter.CacheData() , DataStreamSupporter.Save() , DataStreamSupporter.Save(Stream) , DataStreamSupporter.Save(string) , DataStreamSupporter.Save(string, bool) , DataStreamSupporter.DataStreamContainer , DataStreamSupporter.IsCached , DisposableObject.Dispose() , DisposableObject.Disposed , object.GetType() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

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

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

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.

string dir = "c:\\temp\\";
   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))
       {
           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

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 });
       }
   }

Constructors

SvgImage(string)

Instantiates a new object of the Aspose.Imaging.FileFormats.Svg.SvgImage class, utilizing thespecified path to locate and load the image. This constructor facilitates thecreation of SVG image instances from external files, enabling seamlessintegration into software systems and workflows.

public SvgImage(string path)
   {
   }

Parameters

path string

The path to load image from and initialize pixel and palette data with.

Exceptions

ArgumentNullException

path is null.

SvgImage(Stream)

Creates a new instance of the Aspose.Imaging.FileFormats.Svg.SvgImage class, loading the imagefrom the provided stream. This constructor enables the direct loading of SVGimages from streams, enhancing flexibility and efficiency in handling imageresources within software applications.

public SvgImage(Stream stream)
   {
   }

Parameters

stream Stream

The stream to load image from and initialize pixel and palette data with.

Examples

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

string dir = "c:\\temp\\";
   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))
       {
           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);
       }
   }

Exceptions

ArgumentNullException

stream is null.

SvgImage(int, int)

Instantiates a new Aspose.Imaging.FileFormats.Svg.SvgImage object with the specified width andheight. This constructor allows developers to create SVG images with predefineddimensions, facilitating precise control over the image’s size duringinitialization.

public SvgImage(int width, int height)
   {
   }

Parameters

width int

The image width.

height int

The image height.

SvgImage(SvgOptions, int, int)

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

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

Parameters

svgOptions SvgOptions

The SVG options.

width int

Image width.

height int

Image height.

Properties

BitsPerPixel

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

public override int BitsPerPixel
    {
        get;
    }

Property Value

int

Exceptions

NotImplementedException

Invalid for vector images

FileFormat

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

public override FileFormat
    Get
    {
        get;
    }

Property Value

FileFormat

IsCached

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

public override bool IsCached
{
    get;
}

Property Value

bool

XmpData

Gets or sets XMP data.

public XmpPacketWrapper XmpData
    {
        get;
        set;
    }

Property Value

XmpPacketWrapper

Methods

CacheData()

Cache the data and guarantee that there will be no further loading of data fromthe underlying Aspose.Imaging.DataStreamSupporter.DataStreamContainer. Thisoptimization enhances performance by eliminating redundant data retrievaloperations, especially beneficial in scenarios requiring frequent access to theimage data.

public override void CacheData()
   {
   }

Crop(Rectangle)

Crops the specified rectangle.

public override void
    Crop(Rectangle rectangle)
{
}

Parameters

rectangle Rectangle

The rectangle.

GetImage2Export(ImageOptionsBase, Rectangle, IImageExporter)

Gets the image to export.

protected override Image GetImage2Export(
    ImageOptionsBase optionsBase,
    Rectangle boundsRectangle,
    IImageExporter exporter)
{
}

Parameters

optionsBase ImageOptionsBase

The image options base.

boundsRectangle Rectangle

The bounds rectangle.

exporter IImageExporter

The exporter.

Returns

Image

The image to export

ReleaseManagedResources()

Releases the managed resources. Make sure no unmanaged resources are released here, since they may have been already released.

protected override void ReleaseManagedResources()
   {
   }

Resize(int, int, ResizeType)

Resize the image to fit the specified dimensions while preserving its aspectratio. This method provides a convenient way to adjust the size of the imagewithout distorting its proportions, ensuring optimal display or storageaccording to the desired dimensions.

public override void
    Resize(
        int newWidth,
        int newHeight,
        ResizeType resizeType)
{
}

Parameters

newWidth int

The new width.

newHeight int

The new height.

resizeType ResizeType

The resize type.

Rotate(float)

Rotate image around the center.

public override void Rotate(float angle)
{
}

Parameters

angle float

The rotate angle in degrees. Positive values will rotate clockwise.

SaveData(Stream)

Saves the data.

protected override void SaveData(Stream stream)
   {
   }

Parameters

stream Stream

The stream to save data to.

SetPalette(IColorPalette, bool)

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

public override void
    SetPalette(IColorPalette palette, bool updateColors)
{
}

Parameters

palette IColorPalette

The palette to set.

updateColors bool

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.

Exceptions

NotImplementedException

 English