←Select platform

Parse Method

Summary

Parses the values of a data URI from an href string.

Syntax
C#
VB
Java
Objective-C
C++
public static SvgDataUri Parse( 
   string href 
) 
Public Shared Function Parse( _ 
   ByVal href As String _ 
) As SvgDataUri 
+ (nullable instancetype)parseHRef:(NSString *)hRef error:(NSError **)error 
public static SvgDataUri parse(String href) 
public: 
static SvgDataUri^ Parse(  
   String^ href 
)  

Parameters

href
The href value as a string.

Return Value

Object that contains the parsed values.

Remarks

The Parse method takes an href value as a raw string and tries to parse the data URI uniform resource identifier (URI) members out of it. Refer to SvgDataUri for more information. Parsing is useful for extracting, setting, and converting the pixel data of an image element in an SVG document.

For example, if the image element contains an embedded pixel data as follows:

<image xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA ... etc" />

then Parse will return an SvgDataUri object set up as follows:

Member Value
IsDataUri

true since the parsing was successful and the href contains valid data URI.

Href

Same value as the source href parameter passed to the method.

MediaOffset

5. This is the offset into the href where the media type part starts. In this particular case it is 5, since this is the start of image/png.

MediaLength

9. This is the length of image/png string.

CharSetOffset

-1 since the source href does not contain an explicit charset value. If it did, then this will contain the index into the href where the value starts.

CharSetLength

0 since the source href does not contain an explicit charset value. Otherwise, it will contain the number of characters of the value.

IsBase64

true since the source href states the encoding.

ValueOffset

22. This is the offset into the href where the pixel data of the image starts. In this case 22, since this is the start of the pixel data iVBO... etc.

ValueLength

The length of the pixel data in the href from ValueOffset to the end of the string.

ImageFormat

RasterImageFormat.Png since the media type is image/png which corresponds to this LEADTOOLS raster image format enumeration.

Extension

"png" since this is the common file extension used by the PNG image format.

The reason for using offsets/length pairs instead of getting the values and putting them into properties in the class is because the href can be a very lengthy string containing the pixel data of a large image and making a copy can use unnecessary memory if the user is not interested in the particular value.

To get the values to a separate string, simply use String.Substring passing SvgDataUri.Href and the offset and the length of the desired value. For example, this gets the Media type:

string mediaType = svgDataUri.Href.Substring(svgDataUri.MediaOffset, svgDataUri.MediaLength);

And to get the image data, use:

string base64ImageData = svgDataUri.Href.Substring(svgDataUri.ValueOffset, svgDataUri.ValueLength);

In both cases, checking if the offset is not -1 beforehand in case the object does not contain the value.

The image data is base64 encoded. To convert it to a byte array, use any standard base-64 conversion function such as Convert.FromBase64String or Convert.ToBase64String to encode the data in a byte into a base 64 encoded string.

If the SVG element does not contain a valid data URI but instead has a link to an external resource, for example <image xlink:href="image.png" /> or <image xlink:href="http://server/image.png" /> then calling Parse will succeed, but the value of IsDataUri will be false and the rest of the values of the class should not be used (will be set to their default values.

After successfully parsing (or constructing manually) an SvgDataUri object, you can save the resulting image to a disk file or stream using DecodeToStream or DecodeToFile. For example, with the previous SVG image element with the data URI, calling DecodeToFile will result in a PNG file saved to disk. The Extension property is useful for automatically setting the extension of the file as follows:

var href = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA ... etc"; 
// Parse it. If successful, save it to disk 
var svgDataUri = SvgDataUri.Parse(href); 
if(svgDataUri.IsDataUri) 
   svgDataUri.DecodeToFile("filename." + svgDataUri.Extension); 

The EncodeFromFile and EncodeFromStream methods can create data URI values from image files. For example:

// filename.png is from the previous example 
var href = SvgDataUri.EncodeFromFile("filename.png"); 
// Results in href = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA ... etc" 
// Same as the original value. 

These methods can accept any image file encoded as PNG, GIF, or JPEG. These are the standard file formats used by the web (and by extension, SVG). Any other image format will fail.

Example

For an example, refer to SvgDocument.EnumerateElements

Requirements

Target Platforms

Help Version 20.0.2019.3.12
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2019 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Svg Assembly