Click here to Skip to main content
14,734,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

Can anybody suggest how we can convert image to a byte array and vice versa ?

I'm developing a TCP server and I want transfert a picture but, I think, we have to convert it to byte array.

Thank you in advance.

What I have tried:

I searched on Google but i found only answer on C# language
Posted
Updated 4-Jan-19 6:47am
Comments
Rick York 3-Jan-19 17:41pm
   
First you need to define what form the image data is in. Is it in a file? Is it the pixels in a window? etc...

This is important because the answer depends on what form the data is in.

Take a look at the list of related questions on the right side of the page. There are several questions that look like they could be relevant to you and you might find the answer you need in one of them.

An image is a byte array at its most fundamental: if it is stored in a file (and for C that will almost certainly be the case) then just read the file as binary data and transfer it via your normal data link.

Remember, "an image" is a class description - the actual data content depends on exactly what type of image it is: BMP will will different from PNG, which is very dissimilar to JPG, which doesn't look at all like TIF, which ... you get the idea.

IF you don't just read it and send it, then you need to find a mechanism to read the file, convert it to a "common format" and send that - which will take careful thinking about, because some formats are uncompressed (BMP for example) so transfer times may well be significant; while others use a lossy compression technique (JPG for example) so image quality may well be significantly degraded.

Me? I'd send it as "raw binary data" along with a filename which indicated what type of data it contained.
   
When you have the images as file you may read the bytes into a buffer and transmit them. You must than also take care that the responder is handling the data correct and saves that bytes as images file.

My favorite image processing article is the legendary CXImage. It handles much image processing stuff.

Always remember: an image are only a big bunch of bytes which are interpreted as image. ;-)
   
Comments
RedDk 4-Jan-19 18:13pm
   
Be advised that the link provided is the CP link to it's article. A severe amount of wading through the COMMENT section is required to demuck the compile version disparity boots ... so cutting to the chase (perhaps) see here: https://sourceforge.net/projects/cximage/ for the download.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900