Decode base64 image and display in PictureBox control

  • Thread starter Thread starter Steve Montague
  • Start date Start date
S

Steve Montague

I need to decode a base64 image and display it in a PictureBox control.
Currently I get an "Invaild character in a Base-64 string." error, so not
sure if a bad file or bad code, I have:

byte[] arrBytes = new Byte[5419];

// read the file from disk
System.IO.FileStream fs = System.IO.File.OpenRead("../../Photo.jpg");
arrBytes = new byte[fs.Length];
fs.Read(arrBytes, 0, (int)fs.Length - 1);
fs.Close();

// convert to string, decode and place in control
string s = System.Text.Encoding.ASCII.GetString (arrBytes);
byte[] b = Convert.FromBase64String(s);

// at this a point an exception is throw

System.IO.MemoryStream m = new System.IO.MemoryStream(b);
PhotoPictureBox.Image = System.Drawing.Image.FromStream(m);
______________________________________________________
Question: Should this work and is this the best way?
Thanks for the help! Steve
 
Steve Montague said:
I need to decode a base64 image and display it in a PictureBox control.
Currently I get an "Invaild character in a Base-64 string." error, so not
sure if a bad file or bad code, I have:

byte[] arrBytes = new Byte[5419];

// read the file from disk
System.IO.FileStream fs = System.IO.File.OpenRead("../../Photo.jpg");
arrBytes = new byte[fs.Length];
fs.Read(arrBytes, 0, (int)fs.Length - 1);
fs.Close();

That's bad code in four ways:

1) You're assuming the file length doesn't change
2) You're not even trying to read the final byte
3) You're assuming that Read will read the whole lot in one go,
ignoring the return value
4) You're not closing the stream if an exception is thrown

Instead, use a StreamReader:

string base64;
using (StreamReader reader = new StreamReader
("../../Photo.jpg", Encoding.ASCII))
{
base64 = reader.ReadToEnd();
}

byte[] b = Convert.FromBase64String (base64);
 
Thanks Jon,

Much simpler, but ultimately I will get the encoded file in a byte[] rather
then from disk and will have to process it. Can I still use the
StreamReader? If so how?

Thanks!
_________________________________________________________
Jon Skeet said:
Steve Montague said:
I need to decode a base64 image and display it in a PictureBox control.
Currently I get an "Invaild character in a Base-64 string." error, so not
sure if a bad file or bad code, I have:

byte[] arrBytes = new Byte[5419];

// read the file from disk
System.IO.FileStream fs = System.IO.File.OpenRead("../../Photo.jpg");
arrBytes = new byte[fs.Length];
fs.Read(arrBytes, 0, (int)fs.Length - 1);
fs.Close();

That's bad code in four ways:

1) You're assuming the file length doesn't change
2) You're not even trying to read the final byte
3) You're assuming that Read will read the whole lot in one go,
ignoring the return value
4) You're not closing the stream if an exception is thrown

Instead, use a StreamReader:

string base64;
using (StreamReader reader = new StreamReader
("../../Photo.jpg", Encoding.ASCII))
{
base64 = reader.ReadToEnd();
}

byte[] b = Convert.FromBase64String (base64);
 
Steve Montague said:
Much simpler, but ultimately I will get the encoded file in a byte[] rather
then from disk and will have to process it. Can I still use the
StreamReader? If so how?

How will you get the encoded file in a byte[]? Where will it be coming
from? If it's coming from a stream, then use StreamReader and never
fetch it directly as a byte array. If you *definitely* end up with a
byte array, then you should just call Encoding.ASCII.GetString(byte[]).

(There are probably more memory-efficient ways of working, but that
should be fine.)
 
Back
Top