Programming examples

Here you can copy & paste the code to do this conversion on the most common programming languages that exists. Feel free to share this webpage with all your collegues.

Place this function inside your code to convert an image to base64:

import base64 img_file = "image.png" def image_to_base64(): b64 = base64.encodestring(open(img_file,"rb").read()) # Python 2 return b64 # Python 3 return b64.decode("utf8")

Place this function inside your code to convert a base64 text to an image. img_data should be a the base64 string to be converted to image.

import base64 img_data = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" def base64_to_image(): with open("imageToSave.png", "wb") as fh: fh.write(base64.decodebytes(img_data))

Place this function inside your code to convert an image to base64:

public static void main(String[] args) { File file = new File("C:/Users/<user>/Desktop/image.png"); String encodstring = encodeFileToBase64Binary(file); System.out.println(encodstring); } private static String encodeFileToBase64Binary(File file){ String encodedfile = null; try { FileInputStream fileInputStreamReader = new FileInputStream(file); byte[] bytes = new byte[(int)file.length()]; fileInputStreamReader.read(bytes); encodedfile = new String(Base64.encodeBase64(bytes), "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return encodedfile; }

Place this function inside your code to convert a base64 text to an image. base64Text should be a the base64 string to be converted to image.

private static void base64ToImage(base64Text){ String[] strings = base64String.split(","); byte[] data = DatatypeConverter.parseBase64Binary(strings[1]); String path = "C:\\Users\\<user>\\Desktop\\image.png"; File file = new File(path); try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) { outputStream.write(data); } catch (IOException e) { e.printStackTrace(); } }

Place this function inside your code to convert an image to base64:

public string ImageToBase64(){ string path = "C:\\image.png"; using(System.Drawing.Image image = System.Drawing.Image.FromFile(path)) { using(MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); byte[] imageBytes = m.ToArray(); base64String = Convert.ToBase64String(imageBytes); return base64String; } } }

Place this function inside your code to convert a base64 text to an image:

public System.Drawing.Image Base64ToImage(base64String) { byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); return image; }

Use this code to convert an image to base64:

require "base64" encoded_string = Base64.encode64(open("url_to_image") { |io| io.read })

Use this code to convert a base64 text to an image on Ruby:

File.open('image.png', 'wb') do|f| f.write(Base64.decode64(base_64_encoded_data)) end

Use this function inside your code to convert an image to base64:

function getBase64(url, callback) { var image = new Image(); image.onload = function () { var canvas = document.createElement('canvas'); canvas.width = this.naturalWidth; canvas.height = this.naturalHeight; canvas.getContext('2d').drawImage(this, 0, 0); // Get raw image data callback(canvas.toDataURL('image/png').replace(/^data:image\/(png|jpg);base64,/, '')); }; image.src = url; } getBase64('/image.png', function(base64) { // Do whatever you'd like with the base64! });

Place this code to convert a base64 text to an image and place it on the body:

var image = new Image(); image.src = 'data:image/png;base64,iVBORw0K...'; document.body.appendChild(image);