Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Still A Newbie. I would like to replace the long Base64 string with a variable but keep getting error: Index was outside the bounds of the array.

Scenario: I am retrieving the full base64 string from SQL database e.g data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7

I then split the string for the section that I want that is after the comma ','.

C#
public SqlDataReader reader;
    public String ClientSigImg;
    public String ClientSigImg1;

    ClientSigImg = reader[0].ToString();
    ClientSigImg1 = ClientSigImg.Split(',')[1];

So the above base64 string changes on user input and instead of manual input like below

C#
string base64 = @"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
   byte[] imageBytes = Convert.FromBase64String(base64);

I would like to replace that long string with:

C#
string base64 =ClientSigImg1;
    byte[] imageBytes = Convert.FromBase64String(base64);
Posted

1 solution

Check your data in the DB: almost certainly you have saved it wrong.
If I try your code:
C#
String ClientSigImg;
String ClientSigImg1;

ClientSigImg = @"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
ClientSigImg1 = ClientSigImg.Split(',')[1];
string base64 = ClientSigImg1;
byte[] imageBytes = Convert.FromBase64String(base64);

It works - and I get an array of 43 bytes.

So I would suspect that what you think is valid data in your DB is not: at least one entry does not have a comma. So the Split returns an array of one string, and your indexer fails giving you the "Index was outside the bounds of the array" exception.
 
Share this answer
 
Comments
Uriel Tinashe Patsanza 17-Jun-14 7:39am    
thank you for the response, but i copied the string directly out of the DB

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900