Embed Images with DataURIs in C# & VB PDF Rendering

This article was translated from English: Does it need improvement?
Translated
View the article in English

When working with HTML strings and documents, it is often useful not to depend on a directory of assets. To work around this issue, we use the data URI scheme.

The data URI scheme is a method used in web development to embed data directly into HTML or CSS code, eliminating the need for separate files. Data URIs allow images, files, and even typefaces to be injected directly into an HTML document as a string.

Quickstart: Embed Images in PDFs Using DataURIs

Transform your HTML content into PDF documents easily by embedding images using DataURIs with IronPDF. This quick guide helps developers render PDFs with embedded images using simple, efficient code. With IronPDF, you can seamlessly convert HTML to PDF while maintaining image integrity. Let's dive into a straightforward example that demonstrates how to embed an image directly into your PDF document using DataURI with minimal code.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer.StaticRenderHtmlAsPdf("<img src='data:image/png;base64,...' />").SaveAs("output.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


Basic Image Embedding Example

The following example shows the rendering of an image into an HTML document without an asset file:

:path=/static-assets/pdf/content-code-examples/how-to/datauris-image.cs
using IronPdf;
using System;

// Read byte from image file
var pngBinaryData = System.IO.File.ReadAllBytes("My_image.png");

// Convert bytes to base64
var ImgDataURI = @"data:image/png;base64," + Convert.ToBase64String(pngBinaryData);

// Import base64 to img tag
var ImgHtml = $"<img src='{ImgDataURI}'>";

ChromePdfRenderer Renderer = new ChromePdfRenderer();

// Render the HTML string
var pdf = Renderer.RenderHtmlAsPdf(ImgHtml);

pdf.SaveAs("datauri_example.pdf");
Imports IronPdf
Imports System

' Read byte from image file
Private pngBinaryData = System.IO.File.ReadAllBytes("My_image.png")

' Convert bytes to base64
Private ImgDataURI = "data:image/png;base64," & Convert.ToBase64String(pngBinaryData)

' Import base64 to img tag
Private ImgHtml = $"<img src='{ImgDataURI}'>"

Private Renderer As New ChromePdfRenderer()

' Render the HTML string
Private pdf = Renderer.RenderHtmlAsPdf(ImgHtml)

pdf.SaveAs("datauri_example.pdf")
$vbLabelText   $csharpLabel

We can also serve an entire HTML String or PDF document as a Byte Array using IronPDF's ASP.NET MVC Integration.

Curtis Chau
Escritor Técnico

Curtis Chau tiene una licenciatura en Ciencias de la Computación (Carleton University) y se especializa en el desarrollo front-end con experiencia en Node.js, TypeScript, JavaScript y React. Apasionado por crear interfaces de usuario intuitivas y estéticamente agradables, disfruta trabajando con frameworks modernos y creando manuales bien ...

Leer más
¿Listo para empezar?
Nuget Descargas 16,154,058 | Versión: 2025.11 recién lanzado