How to get an image from its byte array

Hi all,

I have a service that takes an image, converts it into a byte[], then converts it into an ASCII encoded string, and then sends it across a network where it is picked up by my client. I want my client to be able to get the original image back. Is this possible? If not, how must I change it to make it work? Any ideas on how this sort of thing is usually done?

Please let me know if I can clear my question up for you. Any help is greatly appreciated!
ehensensAsked:
Who is Participating?
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
Encoding Strings to Base64 in C#
canecode.com/2007/03/21/encoding-strings-to-base64-in-c/
 static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
  }
 
static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
>>  then converts it into an ASCII encoded string
Base64 encodng?

>>  I want my client to be able to get the original image back
Sure, just reverse what you did on the other machine.

>> Please let me know if I can clear my question up for you.
It'd be good if you can explain what, if anythng, you've done so far and why you think it might be a problem to unencode the image.
0
 
mrjoltcolaCommented:
I normally use base64 encoding. You can use an API that should give you both direction methods, convert binary to base64 and from base64 to binary.

Since an image is a binary blob, there is not always a need to encode it to ASCII, unless it is as part of a text based protocol, as is the case with HTTP/www and/or an attachment. Otherwise, if your network client can just read it in the native byte format, no conversion needed.

Can you clarify why it is converted to ASCII in the first place?

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ehensensAuthor Commented:
Hi evilrix,

The ASCII encoding is being done by C#,

string imageAsString = System.Text.ASCIIEncoding.ASCII.GetString(imageBuffer);

and then my client is an iPhone app, so I'm limited to objective-c++


0
 
ehensensAuthor Commented:
Hi mrjoltcola,

I converted it to ASCII the first time around because I didn't know how else to send it any other way. The service is in C#, and I tried just sending back the byte array itself but the client just read it as "System.Byte[]"
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> The ASCII encoding is being done by C#
Is there any reason you don't just send raw 8 bit bytes (ie binary)? If you must use a text based protocol then use Base64 as it is the standard encoding mechanism for 7 bit ascii.
0
 
mrjoltcolaCommented:
>>The service is in C#, and I tried just sending back the byte array itself

I assume you mean web service? (SOAP or REST/JSON)?
0
 
ehensensAuthor Commented:
yes a web service with SOAP
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> string imageAsString = System.Text.ASCIIEncoding.ASCII.GetString(imageBuffer);
mrjoltcola has far more experience than me with .Net so I'll let him take it from here... I'll monitor and contribute if I think there is anything to add, otherwise over to you mrjoltcola.
0
 
ehensensAuthor Commented:
mrjoltcola,

>>>> The ASCII encoding is being done by C#
>>Is there any reason you don't just send raw 8 bit bytes (ie binary)? If you must use a text >>based protocol then use Base64 as it is the standard encoding mechanism for 7 bit ascii.

Do you know how I would go about doing that?
0
 
ehensensAuthor Commented:
Sorry, i meant to direct that last comment to evilrx
0
 
mrjoltcolaCommented:
What evilrix said... I just saw we are overlapping comments.

I agree. Either use a toolkit that has base64 or write one in objective-c. Very easy to write.

Google base64 + <language of your choice>

Here is a C++ implementation.

http://www.adp-gmbh.ch/cpp/common/base64.html


I personally use a commercial lib called Chilkat
0
 
evilrixSenior Software Engineer (Avast)Commented:
0
 
mrjoltcolaCommented:
Given that you are using SOAP, base64 is what works best inside an XML SOAP envelope. In most cases, the overhead of the encoding isn't a problem, because you are already using a bloated protocol choice in the first place (SOAP).

You can do SOAP with attachments that I think comes in with 1.2 SOAP, and in that you can use 8-bit attachments.

This is really a web-services question, and you might want to bring the web services zone in on it. I am an intermediate in that area. I write them and use them but I'm am not an expert on all the areas.

If you have an established web service that is already returning the ASCII string, I would convert the C# side to return base64.

See the .NET reference:  http://msdn.microsoft.com/en-us/library/system.convert_methods.aspx

Then get the code on the objective-c side working for base64, I imagine there might be an API, but not being an iPhone developer, I cannot say. There appears to be a base64 function in iPhone API:

http://developer.omniture.com/node/321



0
 
ehensensAuthor Commented:
I definitely appreciate all of these comments, but my main concern is once I get the image (no matter what way it ends up being encoded) how do I get it back into an image?
0
 
ehensensAuthor Commented:
Sure, thank you very much.
0
 
mrjoltcolaConnect With a Mentor Commented:
>>my main concern is once I get the image (no matter what way it ends up being encoded) how do I get it back into an image?

Once it is in the un-encoded form, it _is_ the image. How you present/render the image is up to you and is app specific.

How would you load and display an image in objective-c on the iPhone if it existed in a file?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.