Click here to Skip to main content
15,390,197 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to convert image to binary and get result in text box .

i using the following function :

Image found in path D:/person.jpg

C#
public static byte[] ImageToBinary(string _path)  
{  
FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);  
byte[] b = new byte[fS.Length];  
fS.Read(b, 0, (int)fS.Length);  
fS.Close();  
return b;  
}


How to receive the value returned from function ImageToBinary in textbox1 ?

I work in c# windows form c#

What I have tried:

How to convert image to binary and get result in text box
Posted
Updated 12-May-21 23:44pm
Comments
Richard MacCutchan 31-Dec-16 3:46am
   
This question does not make sense. A binary array is not text, so putting it in a text box would just produce unreadable garbage.
ahmed_sa 31-Dec-16 6:22am
   
I need to convert image from byte array to binary string 0 or 1
Ralf Meier 31-Dec-16 7:12am
   
You should explain this ...
What kind of sense does it make for you if you convert each byte of an image to it's binary value (as string) to display it in a Textbox ?
ahmed_sa 31-Dec-16 8:27am
   
yes i need to convert to binary string 0 or 1 and show it in text box

1 solution

You have all the parts, all you need to do is loop through the byte array and convert each number to binary representation.

Keep in mind there is usually a max length on a text box that you might have to manually overcome (make it multi-line / increase the max length, etc.

C#
public static byte[] ImageToBinary(string _path)  
{  
    FileStream fS = new FileStream(_path, FileMode.Open, FileAccess.Read);  
    byte[] b = new byte[fS.Length];  
    fS.Read(b, 0, (int)fS.Length);  
    fS.Close();  
    return b;  
}

public static void SetText(string _path) {
    byte[] b = ImageToBinary(_path);
    
    StringBuilder binary = new StringBuilder();

    foreach (var bb in b) {
         binary.Append(Convert.ToString(bb, 2));
    }

    textBox1.Text = binary.ToString(); 

}
   

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