473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Decode base64 image and display in PictureBox control

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
Nov 16 '05 #1
3 18394
Steve Montague <st************@getronics.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steve Montague <st************@getronics.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Steve Montague <st************@getronics.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ed Landau | last post by:
In VB6.0, I can put an image on a form and assign it's .picture property at run-time. In VBA (within MS Access), when I put down an image control on a form, it asks me for the source to the image...
2
by: barbara_dave | last post by:
Hi, all, I have a form which has a picturebox control on the top and several buttons on the bottom. I want to change the form size smaller at run time and let all buttons display overlap the...
3
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: Deepak C.G via .NET 247 | last post by:
I want to dispose the image object in my child form, unless I won't dispose this object i can't delete the image file in my folder. I get this error in MDIparent form "An unhandled exception...
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
3
by: Kuldeep | last post by:
Hi All, Could you please give me some guidelines on dealing with Base64 encoded string. The actual purpose is to decode Base64 Encoded string and stream the data to a browser so that it can be...
2
by: Frank | last post by:
For various reasons I want to use a PictureBox to display images. That is I want sometimes to assign an Image to the PictureBox Sometimes I want to draw on that Image. What are the reasons for...
1
by: nanaalwi | last post by:
Hi, Currently I'm developing a software using VB.net that can grab an image using Matrox Morphis card and display it in a PictureBox. The software can grab the image already and display it in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.