←Select platform

EncodeFromStream Method

Creates a standard data URI from an image file in a stream.
public static string EncodeFromStream( 
   Stream stream 
+ (nullable NSString *)encodeFromData:(NSData *)data error:(NSError **)error 
public static String encodeFromStream(InputStream stream) 
static String^ EncodeFromStream(  
   Stream^ stream 
def EncodeFromStream(self,stream): 


Stream containing an image file. Supported image formats are PNG, GIF and JPEG. All other image formats will result in an error.

Return Value

A data URI string containing the media type, encoding and image data encoded as base 64.


Refer to Parse for more information about data URIs.

Use EncodeFromFile to create a standard data URI from a file.

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 file 
   var 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 Directory 
   var virtualDirectory = "http://localhost/leadtools_images/svg/resources"; 
   // Assume this phrysical directory maps to the Virtual Directory 
   var physicalDirectory = $@"{LEAD_VARS.ImagesDir}\svg\resources"; 
   if (!Directory.Exists(physicalDirectory)) 
   // Our SVG element enumartion callback 
   SvgEnumerateElementsCallback callback = (SvgDocument document, SvgNodeHandle node, object userData) => 
      if (node.ElementType == SvgElementType.Image) 
         // Get the href 
         var href = node.GetAttributeValue("xlink:href"); 
         if (!string.IsNullOrEmpty(href)) 
            // Parse it as data URI 
            var dataUri = SvgDataUri.Parse(href); 
            // Check if it is a data URI 
            if (dataUri.IsDataUri) 
               // Yes, create a new file in a virtual directory 
               // Show the dataURI properties 
               Console.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)); 
                  Console.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 name 
               var name = Guid.NewGuid().ToString().Replace("-", "") + "." + dataUri.Extension; 
               // Save it 
               // Full physical path 
               var filePath = Path.Combine(physicalDirectory, name); 
               /* Alternatively you can save the data yourself using this code 
               var data = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength); 
               // Save the data 
               var base64String = dataUri.Href.Substring(dataUri.ValueOffset, dataUri.ValueLength); 
               byte[] rawData = Convert.FromBase64String(base64String); 
               // Save it to disk 
               File.WriteAllBytes(filePath, rawData); 
               // Finally replace the attribute in the image element with the URI 
               var virtualPath = virtualDirectory + "/" + name; 
               node.SetAttributeValue("xlink:href", virtualPath); 
               Console.WriteLine("Does not contain a valid data URI."); 
      return true; 
   using (var rasterCodecs = new RasterCodecs()) 
      // Use 300 DPI when loading document images 
      rasterCodecs.Options.RasterizeDocument.Load.Resolution = 300; 
      // Load the first page as SVG 
      using (var svg = rasterCodecs.LoadSvg(sourceFile, 1, null) as SvgDocument) 
         if (!svg.IsFlat) 
         if (!svg.Bounds.IsValid) 
         // Save this SVG to disk, report the size 
         svg.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 process 
         svg.EnumerateElements(new SvgEnumerateOptions { EnumerateDirection = SvgEnumerateDirection.TopToBottom }, callback, null); 
         // Save this SVG to disk again, report the size, should be alot smaller since the image are unembedded and stored as external resources 
         svg.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"; 

Target Platforms

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

Leadtools.Svg Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.