Solved

How to get an image from its byte array

Posted on 2009-05-12
19
683 Views
Last Modified: 2013-12-17
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!
0
Comment
Question by:ehensens
  • 7
  • 5
  • 5
19 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24369453
>>  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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24369461
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
 

Author Comment

by:ehensens
ID: 24369462
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:ehensens
ID: 24369475
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
 
LVL 40

Expert Comment

by:evilrix
ID: 24369484
>> 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
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24369487
>>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
 

Author Comment

by:ehensens
ID: 24369502
yes a web service with SOAP
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24369509
>> 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
 

Author Comment

by:ehensens
ID: 24369512
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
 

Author Comment

by:ehensens
ID: 24369517
Sorry, i meant to direct that last comment to evilrx
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24369524
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
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24369595
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
 
LVL 40

Expert Comment

by:evilrix
ID: 24369598
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24369602
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
 

Author Comment

by:ehensens
ID: 24369605
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
 

Author Comment

by:ehensens
ID: 24369647
Sure, thank you very much.
0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 250 total points
ID: 24369669
>>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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question