Solved

How to get an image from its byte array

Posted on 2009-05-12
19
687 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

726 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