Encode Images online

Encode images with Aspose.SVG for .NET API in real-time! Please load an image from the 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, PJP, PJPEG, PNG, BMP, XBM, GIF, TIFF, ICO, IFIF, WEBP and other image formats are supported.

About URI Scheme

Data URI is a method of embedding images and other files in web pages as a string of text based on Base64 encoding. Data URI scheme allows you to include any binary data in HTML, CSS, JSON, or SVG documents. For example, you can embed images in a web page as if they were loaded from an external resource, but instead of specifying the URL of the file, you insert the Base64 encoded contents of the image. The data URI scheme was defined in RFC 2397, and as of 2022, it is fully supported by most major browsers. The main advantage of data URI use is a speed-up of page loading because the browser does not need to make an additional web request to retrieve the file since the image is already embedded in the HTML document.

The syntax of data URI is the following: data:[<mime type>][;charset=<charset>][;base64],<encoded data>.

Because the size of Base64-encoded data increases by 33%, it is recommended to use data URI only for small images. Large Base64 images produce a lot of code in the HTML, which results in a loss of performance benefits. If you need to convert JPG to Base64 and use encoded string to embed into an HTML, CSS, JSON, or SVG file, please learn all pros and cons first.


How to convert JPG to Base64 in C#

In order to convert JPG 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 JPG 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 JPG to Base64 string and embed it into SVG file

    // Open a binary file – JPG image
    var bytes = File.ReadAllBytes(@"image.jpg");
    // Initialize an SVGDocument object
    var document = new SVGDocument();
    // Create an image element
    var img = (SVGImageElement)document.CreateElementNS("http://www.w3.org/2000/svg", "image");
    // Convert JPG to Base64
    img.Href.BaseVal = "data:image/jpeg;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 JPG to Base64 in C#

  1. Open a JPG image to convert. Use the ReadAllBytes(path) method to open the JPG image and read the contents of the file into a byte array.
  2. Initialize a new instance of the SVGDocument class.
  3. Use the CreateElementNS(namespaceURI, qualifiedName) method of the SVGDocument class to create an image instance. The namespaceURI sets the reference to W3C SVG specification. The qualifiedName must contain the string tag name of the image element.
  4. Convert JPG to Base64. Call The ToBase64String(bytes) method to convert an array of 8-bit integers to its equivalent string representation encoded in base64 digits.
  5. Add the image element into the SVG document using AppendChild(node) method.
  6. Use the Save(path) method to save the SVG document.

Pros and Cons of Data URI

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.

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!

Base64 Encoding Usage Examples

SVG code to embed Base64 JPG image into an SVG document

<svg xmlns="http://www.w3.org/2000/svg">
	<image href="data:image/jpeg;charset=utf-8;base64,/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBA..." alt="Blue circle"/>
</svg>

HTML code to embed Base64 JPG image

<body>
    <div>
        <img src="data:image/jpeg;base64,/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBA..." alt="Blue circle">
    </div>
</body>

CSS code to embed Base64 JPG image as a background image

.class {
    background-image: url('data:image/jpeg;base64,/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBA...');
}

XML code to embed Base64 JPG image into an XML document

<?xml version="1.0" encoding="UTF-8"?>
<root>
<image mime = "image/jpeg">/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBA...</image>
</root>

JSON code to embed Base64 JPG image into JSON document

{
  "image": {
    "mime": "image/jpeg",
    "data": "/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBA..."
  }
}



FAQ

1. How can I convert JPG to Base64?

Aspose.SVG allows you to convert JPG to Base64 string in any way – online or programmatically. For example, you can encode JPG in real-time using Online JPG Encoder that converts your files quickly and with high quality. Upload, encode JPG and get the result in a few seconds! On other hand, you can use Aspose.SVG for .NET API to convert JPG to Base64 programmatically.

2. Why is Base64 Encoding needed?

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.

3. What browsers support Data URIs?

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+.

4. 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.

Get Started with .NET SVG 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:

JPG to Base64 (JPEG Image)
PNG to Base64 (Portable Network Graphics)
BMP to Base64 (Bitmap Image)
SVG to Base64 (Scalable Vector Graphics)
TIFF to Base64 (Tagged Image Format)
GIF TO Base64 (Graphical Interchange Format)
ICO to Base64 (Computer Icons Format)
Image to Base64 (Binary Image)