Created
April 17, 2025 20:32
-
-
Save conholdate-gists/5d00bde90a9a4b94a485955485154327 to your computer and use it in GitHub Desktop.
Convert Image to Base64 | JPG PNG to Base64 Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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