Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get an image from its byte array

Posted on 2009-05-12
19
Medium Priority
?
695 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
17 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 1000 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 1000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

824 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