Click here to Skip to main content
15,616,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

Hi, I'm a total freshy in programming, hope my question make sence.
I'm working on a program which converts image to byte[]
I want the byte code information so that i can use it for further processing
so i convert the byte to string and wrote it into a .txt file

But instead of expected binary numbers, i get these kinda stuff:

㼿①㼍㽰㼥㼢⌿㼈Вୂ⠿♲㼈㼸ሴ㽦䕖㼿ᄿ㼿㼿甞Ԛ䨿㽂㼿㽗㽳㴿፮倿᰿㼐搿㼿〿ℿ㼈晠᥌焈䁮戌㼿ਬ
└㑿ቮ〿䐿␿簿ᄤ㽁⥾㼍㼐ࠨᠢⴿ愄㼸ᡒ㼢䀿͖㼹㼹㽢䐿㼿㼿㼿ᠿ㼿㽳㼿㼿㼿㼿挗㼎ဓ࠿㼜ℜ㼀᝸㼃ᠿ㼿㼿п㼱
㼿ᄿ㼿ͪ渿㼔兘䀋␿瀿฿㼁⁣硁ࠞ㽀㼅簿䀋ᴿ䀿ᐿ堂ᅈ㼿㼿ਿ㼖㽳儿∎愔⌿䀃࠹㽡㽝吿ȕ␈䌄砸㼿⌿㼃ᨃ
ℍ⸸兆┿〿㽄稿㼳㽸Ȕ㤼ㄍ㔿䈿欿㼿㼝ᨁ㼣㼿㼋䜿㼿ဿ硁ᱡ㼂㼿樁㼿㽀㼕嘴㼿ᅥ㽤㼿ᡄݐ婤㼎㽹̡㤿㼃
䙄㼸戔怿焋㴿᠇ᠿ䁂㼰㼿䈓尨ℊ⌿㼄ᴿ㽃᜿摃㼿᰿㼼㽓䈖㼿㼏̿ဿ฿㽁㽟㼿đ㥨愍眿㼿ㄿ⠿㼿㽳㽶㽱Ġ刿䄈
ဿ㼿㼽昰∊瀿Kᔿ恁၏㼿㼭㼐㼙㼀愋ℼ瀃ၚ㼿ٽ㽀З怿㼎夐⌿㼿儐㜿砿ሿ㽂㽻㼿ܿ㼿㼅൘㼁ᤸ᥅㴿㼓匿㼿


Below is my C# code:

//this function convert image to byte array then stored to byte[] called haha
byte[] haha = imageToByteArray(pictureBox1.Image);

//this part of the operation convert byte array to string called hihi
string hihi;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
hihi = enc.GetString(haha);

//this part of the operation write hihi into .txt file
string codetxtfile = "C:/Users/Trainee/Desktop/Test.txt";
System.IO.StreamWriter objWriter;
objWriter = new System.IO.StreamWriter(codetxtfile);
objWriter.Write(hihi);
objWriter.Close();

MessageBox.Show("Done!");

How do i convert the image to byte and show it in binary code in .txt file? Please

Question Update:


I'm trying to convert the pixel color data on a 8 bit depth grayscale image(256 types of black to white) to numbers, i want these numbers to be in proper arrangement according to the pixel, as in lines of pixels in the image will be converted to line of numbers.

For eg.

0= black, 255= white , 1-254= intermediate grey between black and white

So,  for a gray scale image

i want the generated code to be like:

0811FF00...... (in hexadecimal), where 08= 8th grey, 11= 17th grey, FF=white, and 00= black

or

00001000000101111111111100000000..... (in binary code), where 00001000= 8th grey, 00010111=17th grey, 11111111= white, and 00000000= black

Previosly, i thought image to byte conversion will give arrays of binary numbers(each represent a pixel depth), i guess i was wrong.

Anyone can guide me in this please?

Posted
Updated 24-Nov-09 18:40pm
v6

I'm working on a program which converts image to byte[]

 

If you read my image processing articles, you'll find how to read the bytes of an image.  However, I still maintain that if you're totally new to programming, this is an insane task.  Are you being paid for this, is it for school, or are you setting yourself a task that is unreasonable for your own interest?

In addition, you realise if you edited your original question, it would jump to the top of the queue ? That's a much better way to consolidate a discussion IMO.

 
Share this answer
 
v2
For further background on this, check out the previous question. Notice that this person seems to be working with grayscale, which can be represented by a byte (values 0-255)

You might want to tell us more about what you are trying to accomplish. Are you trying to convert the pixel color data to numbers, then convert those numbers to human readable text? Or, are you trying to convert the pixel color data to numbers, then store those numbers to a file as byte data (which would not be human readable, but which could be loaded into a program later on)? Seems like you may be confusing the two.
 
Share this answer
 

I don't know C#, but maybe you can translate this old VB5 code I did many years ago. I think it will do what you're trying to do. Here's the way it works:

  • open the picture file for binary input
  • read a string of bytes (I chose to read in 16 at a time, but you can make it anything you want.
  • convert each byte in the string to its hex representation and add it to an output string.
  • loop through the input string until all bytes have been converted
  • write the output string to a file
  • loop to read the next string of bytes from the input file.

Here's the VB code:

Dim txtIn As String, txtOut As String, txtChr As String
Dim n As Integer

Open "C:\Picture1.tif" For Binary As #1
Open "c:\temp\HEXOut.txt" For Output As #2

'Loop until there is no more to read from the input file

Do While Not LOF(1)

'read 16 bytes from the input (picture) file (#1)
txtIn = Input(16, #1)

'initialize txtOut to an empty string

txtOut = ""

'Loop through txtIn while converting each character (byte)

For n = 1 To Len(txtIn)

'This line uses three functions built into VB5:

' 1. Mid() returns individual characters in a string. In this case it returns a single character (1) at the nth position in the txtIn string

' 2. Asc() returns the ASCII code for a single-character string.

' 3. Hex() converts a decimal value to its hex representation and returns it as a character string

txtChr = Hex(Asc(Mid(txtIn, n, 1)))

'I add a leading zero to hex numbers < 16 (0-F). Otherwise they would be represented as single digits and be harder to read.

If Len(txtChr) < 2 Then txtChr = "0" & txtChr

'add the HEX characters to the output string

txtOut = txtOut & txtChr

Next n

'This line just adds a carriage return and linefeed to the line to make the file easier for humans to read. The output file contains lines of 16 two-character bytes.

txtOut = txtOut & vbCrLf

'Write the line to the output file

Print #2, txtOut

'Loop until the whole file has been read.

Loop

Close

I hope this helps. Sorry I couldn't translate it. One of these days I might try to learn C#, but not today.

-tom-

 
Share this answer
 
v4


I'm trying to convert the pixel color data on a 8 bit depth grayscale image(256 types of black to white) to numbers, i want these numbers to be in proper arrangement according to the pixel, as in lines of pixels in the image will be converted to line of numbers.

For eg.

0= black, 255= white , 1-254= intermediate grey between black and white

So, i for a gray scale image

i want the generated code to be like:

0811FF00...... (in hexadecimal),                                                       where 08= 8th grey, 11= 17th grey, FF=white, and 00= black

or

00001000000101111111111100000000..... (in binary code),                      where 00001000= 8th grey, 00010111=17th grey, 11111111= white, and 00000000= black

Previosly, i thought image to byte conversion will give arrays of binary numbers(each represent a pixel depth), i guess i was wrong.

Anyone can guide me in this please?

 
Share this answer
 

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