Represents a data URI object.
public class SvgDataUri SvgDataUri is a class that handles the data URI uniform resource identifier (URI) scheme for including data in-line in SVG files. The syntax for Data URI is:
data:[<media type\>][;charset=\<character set>][;base64],<data>Refer to Data URI scheme for more information.
In SVG files, an image element comes in the following form:
<image x="x-position" y="y-position" width="width-value" height="height-value" xlink:href="image-data" />Where x-position, y-position, width-value and height-value are the optional location and size of the image inside the SVG.
image-data can have one of the following formats:
A link to an external resource. For example:
xlink:href="image.png" or xlink:href="http://server/myimage.png"A data URI that includes the image pixel data embedded inside the SVG:
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA ... etc"Refer to https://www.w3.org/TR/SVG/ for more information about the SVG image element.
When an SVG document is flattened, all image elements will be embedded in the SVG as data URI elements. This allows the SVG file to be self-contained and passed from a server to a client without relying on any external resources.
Extracting data from a data URI or constructing a new one from raw image data is not supported by the .NET framework directly and the SvgDataUri class contains functionality that allows the user to:
Given a data URI value as a string (as in the example above), extract all relevant data such as the media type, character set, encoding and the data itself using the Parse method. Some of these parameters are optional and some come in certain formats and the Parse method can handle all of the features supported by data URI.
Encode a data URI object from an image in a file or stream using EncodeFromFile and EncodeFromStream. These methods return a data URI string value that is ready to be inserted into an SVG file.
Save the value of an image data URI to a file or stream using DecodeToFile and DecodeToStream using the URI's media type and pixel data. These methods allow the user to extract any embedded image from an SVG document.
using Leadtools;using Leadtools.Codecs;using Leadtools.Drawing;using Leadtools.Forms.DocumentWriters;using Leadtools.Svg;using Leadtools.Document.Writer;private static void SvgDocumentEnumerateElementsExample(){// The source PDF filevar sourceFile = $@"{LEAD_VARS.ImagesDir}\Leadtools.pdf";var beforeSvgFile = $@"{LEAD_VARS.ImagesDir}\Leadtools_before.svg";var afterSvgFile = $@"{LEAD_VARS.ImagesDir}\Leadtools_after.svg";// Assume this is our Virtual Directoryvar virtualDirectory = "http://localhost/leadtools_images/svg/resources";// Assume this phrysical directory maps to the Virtual Directoryvar physicalDirectory = $@"{LEAD_VARS.ImagesDir}\svg\resources";if (!Directory.Exists(physicalDirectory))Directory.CreateDirectory(physicalDirectory);// Our SVG element enumartion callbackSvgEnumerateElementsCallback callback = (SvgDocument document, SvgNodeHandle node, object userData) =>{if (node.ElementType == SvgElementType.Image){// Get the hrefvar href = node.GetAttributeValue("xlink:href");if (!string.IsNullOrEmpty(href)){// Parse it as data URIvar dataUri = SvgDataUri.Parse(href);// Check if it is a data URIif (dataUri.IsDataUri){// Yes, create a new file in a virtual directory// Show the dataURI propertiesConsole.WriteLine("Replacing data URI");Console.WriteLine("Format:" + dataUri.ImageFormat);if (dataUri.MediaLength > 0)Console.WriteLine("Media:" + dataUri.Href.Substring(dataUri.MediaOffset, dataUri.MediaLength));if (dataUri.CharSetOffset > 0)Console.WriteLine("CharSet:" + dataUri.Href.Substring(dataUri.CharSetOffset, dataUri.CharSetLength));elseConsole.WriteLine("CharSet: not set");Console.WriteLine("IsBase64:" + dataUri.IsBase64);Console.WriteLine("ImageFormat:" + dataUri.ImageFormat);var extension = dataUri.Extension;Console.WriteLine("Extension:" + dataUri.Extension);// Get a file namevar name = Guid.NewGuid().ToString().Replace("-", "") + "." + dataUri.Extension;// Save it// Full physical pathvar filePath = Path.Combine(physicalDirectory, name);dataUri.DecodeToFile(filePath);/* Alternatively you can save the data yourself using this codevar data = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength);// Save the datavar base64String = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength);byte[] rawData = Convert.FromBase64String(base64String);// Save it to diskFile.WriteAllBytes(filePath, rawData);*/// Finally replace the attribute in the image element with the URIvar virtualPath = virtualDirectory + "/" + name;node.SetAttributeValue("xlink:href", virtualPath);}else{Console.WriteLine("Does not contain a valid data URI.");}}}return true;};using (var rasterCodecs = new RasterCodecs()){// Use 300 DPI when loading document imagesrasterCodecs.Options.RasterizeDocument.Load.Resolution = 300;// Load the first page as SVGusing (var svg = rasterCodecs.LoadSvg(sourceFile, 1, null) as SvgDocument){if (!svg.IsFlat)svg.Flat(null);if (!svg.Bounds.IsValid)svg.CalculateBounds(false);// Save this SVG to disk, report the sizesvg.SaveToFile(beforeSvgFile, null);Console.WriteLine("Before unembedding the image, size is " + new FileInfo(beforeSvgFile).Length);// Now enumerate the elements to replace each embedded image with a URL// Since we are going to modify the SVG, call BeginUpdate/EndUpdate to speed up the processsvg.BeginUpdate();svg.EnumerateElements(new SvgEnumerateOptions { EnumerateDirection = SvgEnumerateDirection.TopToBottom }, callback, null);svg.EndUpdate();// Save this SVG to disk again, report the size, should be alot smaller since the image are unembedded and stored as external resourcessvg.SaveToFile(afterSvgFile, null);Console.WriteLine("Before unembedding the image, size is " + new FileInfo(afterSvgFile).Length);}}}static class LEAD_VARS{public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";}