Reputation: 109
I have found this code:
// provide read access to the file
FileStream fs = new FileStream(media.Path, FileMode.Open,FileAccess.Read);
// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];
//Read block of bytes from stream into the byte array
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
string _base64String = Convert.ToBase64String (ImageData);
I'm not sure how to pass in an image and which imports to use to get it working. I basically want to take an image and convert it to text.
Thanks
Upvotes: 0
Views: 1150
Reputation: 3609
I recommend storing images in a blob file. But if you really want to store them in a string you can achieve it with the following code snippets
To string
byte[] ImageData = File.ReadAllBytes(@"Path to the image to load");
string _base64String = Convert.ToBase64String(ImageData);
To byteArray
byte[] data = Convert.FromBase64String(_base64String);
File.WriteAllBytes(@"Path to the image to store", data);
Upvotes: 2