Solved

How to get an image from its byte array

Posted on 2009-05-12
19
681 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now