TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ToBase64String (Data URI Image)

Use ToBase64String to take an image and converts it to a base 64 string. Encode an image in a data URI.
ToBase64String. Any kind of binary data (like a JPG image) can be stored in a Base 64 representation. This encoding is within the range of ASCII text.
In C# programs we can use the Convert type that is found in the System namespace. We directly invoke the ToBase64String method. We can pass in a byte array.Using SystemByte Array
An example. Consider this short program. We have a path to an image on the local disk (the image is named "coin.jpg" but you can change the file name and the path).

ToBase64String: This method is where the work happens. We pass in a byte array (the image data itself) and it returns a Base 64 string.

Tip: The Base 64 image data can be used in a Data Uri inside an HTML web page, or in any other text document.

C# program that converts byte array to Base64 string using System; using System.IO; class Program { static void Main() { // The path of the image. string image = @"C:\programs\coin.jpg"; // ... Read in bytes of image. byte[] data = File.ReadAllBytes(image); // ... Convert byte array to Base64 string. string result = Convert.ToBase64String(data); // ... Write Base64 string. Console.WriteLine("ENCODED: " + result); } } Output ENCODED: /9j/2wBDAAQDAwQDAwQEAwQFBAQFBgoHBgYGBg0JCggKDw0QE [Truncated]
Data URI. We can place the image directly in an HTML file with the data URI syntax. This eliminates a HTTP request when a user views the page with the image.

Here: We encode an image to base 64, and then put that encoded data into an HTML file. To body element has a background image.

Steps: Modify the file name to point to an image on your disk. Adjust the output file name. Then open the HTML file in a browser.

C# program that creates data URI image in HTML using System; using System.IO; class Program { static void Main() { // Update this path. string image = @"C:\Codex\i\2d-adventurers-map-background.jpg"; byte[] data = File.ReadAllBytes(image); // Get html string containing image. string result = @"<html><body style=""background:url(data:image/jpeg;base64," + Convert.ToBase64String(data) + @"""></body></html>"; // Write the string. File.WriteAllText(@"C:\programs\image.html", result); Console.WriteLine("[DONE]"); } } Output [DONE]
Notes, ReadAllBytes. In C# programs the easiest way to get binary data for an image (or other similar file) is to use the ReadAllBytes method. It receives a path, and returns a byte array.File.ReadAllBytesPath
A summary. The Convert.ToBase64String method is used in the creation of this website. It returns the same results as a similar method for the Go language.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf