Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created April 17, 2025 20:32
Show Gist options
  • Save conholdate-gists/5d00bde90a9a4b94a485955485154327 to your computer and use it in GitHub Desktop.
Save conholdate-gists/5d00bde90a9a4b94a485955485154327 to your computer and use it in GitHub Desktop.
Convert Image to Base64 | JPG PNG to Base64 Image
// Load an input JPG image
var bytes = File.ReadAllBytes(@"C:\Files\Sample_JPG.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 image to Base64
img.Href.BaseVal = "data:image/png;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(@"C:\Files\image-base64.svg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment