Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...