How to convert an image to a Base64 string and back?
-
I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:
ImageConverter converter = new ImageConverter();
byte[] raw = new byte[1];
Bitmap test = new Bitmap(OFD_Open.FileName);raw = (byte[])converter.ConvertTo(test, typeof(byte[]));
string output = Convert.ToBase64String(raw);
File.WriteAllText("image.txt", output);And this code to convert it from a string back to an image
byte[] raw = new byte[1];
Bitmap pic = new Bitmap(1, 1);
raw = Convert.FromBase64String(OFD_Open.FileName);
PictureBox box = new PictureBox();TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
pic = (Bitmap)tc.ConvertFrom(raw);But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.
-
I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:
ImageConverter converter = new ImageConverter();
byte[] raw = new byte[1];
Bitmap test = new Bitmap(OFD_Open.FileName);raw = (byte[])converter.ConvertTo(test, typeof(byte[]));
string output = Convert.ToBase64String(raw);
File.WriteAllText("image.txt", output);And this code to convert it from a string back to an image
byte[] raw = new byte[1];
Bitmap pic = new Bitmap(1, 1);
raw = Convert.FromBase64String(OFD_Open.FileName);
PictureBox box = new PictureBox();TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
pic = (Bitmap)tc.ConvertFrom(raw);But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.
Hi, the Convert.FromBase64String expect a base64-encoded string, which is in your case stored within a file. But you pass the name of the file. You have to pass the content of the file!
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:
ImageConverter converter = new ImageConverter();
byte[] raw = new byte[1];
Bitmap test = new Bitmap(OFD_Open.FileName);raw = (byte[])converter.ConvertTo(test, typeof(byte[]));
string output = Convert.ToBase64String(raw);
File.WriteAllText("image.txt", output);And this code to convert it from a string back to an image
byte[] raw = new byte[1];
Bitmap pic = new Bitmap(1, 1);
raw = Convert.FromBase64String(OFD_Open.FileName);
PictureBox box = new PictureBox();TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
pic = (Bitmap)tc.ConvertFrom(raw);But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.
Megidolaon wrote:
raw = Convert.FromBase64String(OFD_Open.FileName);
I think converting the filename won't help much... If I were you I would convert the content! From the top of my mind perhaps this will work: Convert.FromBase64String(File.ReallAllText(OFD_Open.FileName));
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:
ImageConverter converter = new ImageConverter();
byte[] raw = new byte[1];
Bitmap test = new Bitmap(OFD_Open.FileName);raw = (byte[])converter.ConvertTo(test, typeof(byte[]));
string output = Convert.ToBase64String(raw);
File.WriteAllText("image.txt", output);And this code to convert it from a string back to an image
byte[] raw = new byte[1];
Bitmap pic = new Bitmap(1, 1);
raw = Convert.FromBase64String(OFD_Open.FileName);
PictureBox box = new PictureBox();TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
pic = (Bitmap)tc.ConvertFrom(raw);But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.
Hi, I have no experience with ImageConverter and have never seen any code using it. What most people do is save the image to a MemoryStream, get its byte array, call Convert.ToBase64String; then do the opposite to get it back. The advantage is the code really is very symmetrical, which is great as you want it to behave symmetrically too. BTW: your initializations with dummy data/sizes don't make sense, just leave them out. I mean the new byte[1] and new Bitmap(1,1) :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
I want to store images in text form and later load them again. I got this code to convert and image to a base64 string:
ImageConverter converter = new ImageConverter();
byte[] raw = new byte[1];
Bitmap test = new Bitmap(OFD_Open.FileName);raw = (byte[])converter.ConvertTo(test, typeof(byte[]));
string output = Convert.ToBase64String(raw);
File.WriteAllText("image.txt", output);And this code to convert it from a string back to an image
byte[] raw = new byte[1];
Bitmap pic = new Bitmap(1, 1);
raw = Convert.FromBase64String(OFD_Open.FileName);
PictureBox box = new PictureBox();TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
pic = (Bitmap)tc.ConvertFrom(raw);But the Convert.FromBase64String method throws an error, telling me there is an invalid character in the Base64 string. What went wrong? Thanks.
:doh: Wow, that was really stupid. I totally forgot to read the file content. Thanks. It really helps to have someone else look at your code, you sometimes overlook the most basic things.