Convert an Image (Selected by Path) to Base64 String

Convert an image (selected by path) to base64 string

Get the byte array (byte[]) representation of the image, then use Convert.ToBase64String(), st. like this:

byte[] imageArray = System.IO.File.ReadAllBytes(@"image file path");
string base64ImageRepresentation = Convert.ToBase64String(imageArray);

To convert a base64 image back to a System.Drawing.Image:

var img = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64String)));

Convert Image to Base64 string


StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile file1 = await storageFolder.GetFileAsync("Image.png");

string _b64 = Convert.ToBase64String(File.ReadAllBytes(file1.Path));

This worked for me.

How to convert image from path to Base64 in flutter?

print function did not print everything.

you can see full result with debug mode

and copy paste full result in debug mode to online tool

Sample Image

Sample Image

How can I convert an image into a Base64 string?

You can use the Base64 Android class:

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

You'll have to convert your image into a byte array though. Here's an example:

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the bitmap object
byte[] b = baos.toByteArray();

* Update *

If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 AKA version 2.2).

Check this article out for a workaround:

How to base64 encode decode Android

Way to convert image straight from URL to base64 without saving as a file in Python

Using the requests library:

import base64
import requests


def get_as_base64(url):

return base64.b64encode(requests.get(url).content)

how can i convert image uri to base64?

You can use react-native-fs if you are using react-native cli

documentation: https://github.com/itinance/react-native-fs#readfilefilepath-string-encoding-string-promisestring

import RNFS from 'react-native-fs';

//base64 res
var data = await RNFS.readFile( "file://path-to-file", 'base64').then(res => { return res });


Related Topics



Leave a reply



Submit