Encode Images online
Encode images with Aspose.SVG in real-time! Please load an image from your local file system and you will immediately get the result as a data URI, Base64 Image Source, and Base64 CSS Background Source. SVG, JPG, JPEG, PNG, BMP, XBM, GIF, TIFF, ICO, WEBP and other image formats are supported.
What is Data URI Scheme?
Data URI is a method for embedding images and other files directly into web pages as Base64-encoded text. The Data URI scheme allows you to include binary data in HTML, CSS, JSON, or SVG documents without referencing external files. For example, instead of linking to an image via a URL, you can insert its Base64-encoded content directly into the markup. The key advantage is faster page loading, as the browser does not need to make additional HTTP requests – the resource is already embedded in the document.
The syntax of data URI is the following: data:[<mime type>][;charset=<charset>][;base64],<encoded data>.
SVG files may contain “unsafe” characters. Vector images can have raster as embedded content or attributes whose values are enclosed in single or double quotes. Therefore, preliminary processing (normalization) of this format is required. You can use SVG in HTML or CSS via data URI. If SVG is encoded using data URI, it will work in any browser.
How to convert SVG to Base64 in C#
In order to convert SVG image to Base64 string, we use Aspose.SVG for .NET API, which is a feature-rich, powerful, easy-to-use document manipulation API for the C# platform. We consider the example of SVG image to Base64 encoding and embedding Base64 string as data URI into an SVG document. These operations can be done with a few lines of code:
C# code to convert SVG to Base64 string and embed it into SVG file
// Open an SVG image
byte[] bytes = File.ReadAllBytes(@"flower.svg");
// Initialize an SVGDocument object
SVGDocument document = new SVGDocument();
// Create an image element
SVGImageElement img = (SVGImageElement)document.CreateElementNS("http://www.w3.org/2000/svg", "image");
// Convert SVG image to Base64
img.Href.BaseVal = "data:image/svg+xml;charset=utf-8;base64," + Convert.ToBase64String(bytes);
// Add the image element into the SVG document
document.RootElement.AppendChild(img);
// Save the SVG document
document.Save(@"image-base64.svg");
Steps to Convert SVG to Base64 in C#
- Open an SVG image to convert. Use the ReadAllBytes(
path) method to open the SVG image and read the contents of the file into a byte array. - Initialize a new instance of the SVGDocument class.
- Use the
CreateElementNS(
namespaceURI, qualifiedName) method of the SVGDocument class to create an image instance. ThenamespaceURIsets the reference to W3C SVG specification. ThequalifiedNamemust contain the string tag name of the image element. - Convert SVG to Base64. Call The ToBase64String(bytes) method to convert an array of 8-bit integers to its equivalent string representation encoded in base64 digits.
- Add the image element into the SVG document using
AppendChild(
node) method. - Use the
Save(
path) method to save the SVG document.
Code Examples: Embed Base64 SVG in SVG, HTML, CSS, XML, JSON
SVG code to embed Base64 SVG image into an SVG document
<svg xmlns="http://www.w3.org/2000/svg">
<image href="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB3aWR0aD0iNDUwIiBoZWlna..." alt="Blue flower"/>
</svg>
SVG <image> – Embed base64 images directly into vector graphics compositions or icon sets.
HTML code to embed Base64 SVG image
<body>
<div>
<img src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDUwIiBoZWlna..." alt="Blue flower">
</div>
</body>
HTML <img> – Display images inline on web pages without triggering additional HTTP requests.
CSS code to embed Base64 SVG image as a background image
.class {
background-image: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDUwIiBoZWlna...');
}
CSS background-image – Use for decorative UI elements, buttons, and responsive background patterns.
XML code to embed Base64 SVG image into an XML document
<?xml version="1.0" encoding="UTF-8"?>
<root>
<image mime = "image/svg+xml">PHN2ZyB3aWR0aD0iNDUwIiBoZWlna...</image>
</root>
XML – Ideal for configuration files, legacy system integration, and structured data exchange.
JSON code to embed Base64 SVG image into JSON document
{
"image": {
"mime": "image/svg+xml",
"data": "PHN2ZyB3aWR0aD0iNDUwIiBoZWlna..."
}
}
JSON – Perfect for REST API responses, mobile application payloads, and modern web services.
Pros and Cons of Data URI for SEO and Performance
The data URI gives us a wise way to embed images on a web page. URI schemes can be used in many ways. But in any case, there are pros and cons.
Pros:
- Speeding up page loading. The browser doesn’t need to make an additional web request to retrieve the file because the image is already embedded in the HTML document.
- Performance improvement. The browser requires less CPU time.
- Web pages become independent of external files, making them easy to share even offline.
Cons:
- The size of Base64 encoded data is 1/3 larger than the binary image.
- The encoded images are not cached by the browser and will be downloaded each time such a page is visited.
- Encoded images are difficult to edit because the Base64 string needs to be decoded first.
- A Base64 image will never be indexed by Google because it doesn’t appear in image search results.
Recommendation:
- Choose Base64 for: small icons, critical above-the-fold assets, offline applications, and self-contained templates.
- Choose external files for: photographs, image galleries, user-generated content, SEO-focused pages, and large media files.
Online Base64 Encoders
Aspose.SVG offers free online applications for encodind and decoding binary data:
- Base64 Encoders are a set of tools that allow you to encode binary data in various output formats: Plain Base64, JSON, XML, URI, or CSS.
- Image Base64 Decoder converts a data URI containing a Base64 string to an image by pasting a URI string in the input control.
Our browser-based applications work from all platforms, including Windows, Linux, Mac OS, Android and iOS. No registration, plugin or software installation required for you. Start using our online Base64 encoding/decoding tools safely, secure, and easy way!
What is Base64 encoding?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. Base64 encoding is commonly used when it is necessary to encode binary information that needs to be stored and transferred over media designed to work with textual data.
Historically, many data transfer and storage formats use text instead of binary code (HTML, XML, e-mail, and so on). What if the data transfer format is text-based, but you require to transfer the binary data? This is where Base64 encoding comes in. To prevent misinterpretation of binary symbols when sending data over the Internet, it is necessary to encode them into characters. Some typical applications of Base64 Encoding: data URI schemes for images (CSS, HTML, JavaScript); transferring pictures and other binary data to XML; storing images in a database; including images into email messages.
FAQ
No. Base64 is a lossless encoding scheme – it only changes how data is represented, not the data itself. The decoded image is byte-for-byte identical to the original. Quality loss can only occur if you compress the source SVG image before encoding.
Use Base64 for small, critical assets (icons, logos, email graphics) where reducing HTTP requests matters. Avoid for large SVG images – external files with caching perform better.
Data URI is the fastest and easiest way to embed images and other files on your web pages. Data URI is supported by major modern browsers: Chrome, Firefox, Safari, Edge, Opera, and IE8+.
Base64 images won’t appear in Google Image Search, but they’re fully indexable for page content. Add alt text for accessibility and SEO benefits.
Get Started with Aspose.SVG for .NET API
If you are interested in developing scalable vector graphics and their application, install our flexible, high-speed Aspose.SVG for .NET API with a powerful set of interfaces for C# and other .NET programming languages.
Install from command line as nuget install Aspose.SVG or via Package Manager Console of Visual Studio with Install-Package Aspose.SVG.
Alternatively, get the offline MSI installer or DLLs in a ZIP file from
downloads.
Aspose.SVG for .NET API is a standalone library and does not depend on any software for SVG document processing.
For more details about C# library installation and system requirements, please refer to
Aspose.SVG Documentation.
Other Supported Base64 Encoders
You can convert Image to Base64 string – JPG, PNG, BMP, GIF, TIFF, ICO, and SVG formats are supported: