If you ever ask yourself how to convert images to characters or to binary. This tutorial will show you how to make this on the easiest way posible by arrays.
Creating the Form
- Open the visual studio, create a new form, windows application.
- Drag the follow items to the form: Two Buttons, One pictureBox, One TextBox and one OpenDialog.
- Make the TextBox Multiline on the properties page.
- Be sure that the PictureBox is an square because we're going to work with square arrays.
Your form should look like the one in the picture 1.

Inserting an Image to the Picture Box
To insert the image double click on one of the two buttons, this will lead you to the click event, and then try the next code:
- openFileDialog1.FileName = ""; // Setting the opefiledialog to default
- openFileDialog1.Title = "Images"; // Title of the dialog
- openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png"; // In the filter you should write all the types of image
- // that you would like to open separete by |, example: "Png Images| *.png"
- openFileDialog1.ShowDialog(); // This show the dialog
- if (openFileDialog1.FileName.ToString() != "") // we check that theres a file selected.
- {
- Imagen.ImageLocation = openFileDialog1.FileName.ToString(); //Setting the pictureBox.Imagelocation to the
- direction.
- }
Voila the Picture Box will display the image you select.
Converting the image to binary
Ok this is how I normally works with arrays and images. First we should create a global variable:
- Bitmap img;
This one let us work with the image withou modify the original. Then you should add some code after the,
- "Imagen.ImageLocation = openFileDialog1.FileName.ToString();"
- img = new Bitmap(openFileDialog1.FileName.ToString()); // Make a copy of the image in the Bitmap variable
Then go back to the design form and double click on the second button, this will lead you to the event, and then place the next code:
- // First we declare the variable that will make the strig that will be printed in the textbox
- string texto = "";
- //The we make the cicles to read pixel by pixel and we make the comparation with the withe color.
- for (int i = 0; i < img.Height; i++) {
- for (int j = 0; j < img.Width; j++) {
- //When we add the value to the string we should invert the
- order because the images are reading from top to bottom
- //and the textBox is write from left to right.
- if (img.GetPixel(j, i).A.ToString() == "255" && img.GetPixel(j, i).B.ToString() == "255" && img.GetPixel(j, i).G.ToString() == "255" && img.GetPixel(j, i).R.ToString() == "255") {
- texto = texto + "0";
- } else {
- texto = texto + "1";
- }
- }
- texto = texto + "\r\n"; // this is to make the enter between lines
- }
- //And finally we put the string to the textbox
- txtArreglo.Text = texto;
As you can see in picture 2 the image will display only zeros and ones, if the pixel color is diferent than white.

If your image is big the delay of execution will be long because this code search pixel by pixel.


Raja RajuPosted May 17, 2018, 8:59 AM EDT
I tried but getting errors ..please share the video link here..plsssss
Raja RajuPosted May 17, 2018, 8:58 AM EDT
Can you explain the same through video..
Hussain BadushaPosted Jan 14, 2017, 7:51 PM EST
Hi, thanks for the post. But i'm getting error array index out of bound.
mostafa fayadeditedPosted May 20, 2013, 7:01 PM EDTEdited May 20, 2013, 7:02 PM EDT
how can i convert it again into image? and thank you for your great work
FerdousPosted Feb 27, 2013, 3:59 PM EST
I got the Error: ArgumentOutOfRangeException was unhandled Parameter must be positive and < Height. Parameter name: y what does it mean? please help.
choey gamboaPosted Mar 24, 2012, 7:09 AM EDT
sir, what about converting a template to binary?
chathuri buddhimaeditedPosted May 26, 2011, 1:14 AM EDTEdited May 26, 2011, 1:41 AM EDT
i got outofRangeException in code if (img.GetPixel(i, j).A.ToString() == "250" && img.GetPixel(i,j).B.ToString() == "250" && img.GetPixel(i, j).G.ToString() =="250" && img.GetPixel(i, j).R.ToString(); pl give me a solution i got the solution ....thanks u very much
jitendra madanPosted Apr 26, 2011, 11:44 PM EDT
it's very good...
ayushi aggeditedPosted Apr 19, 2011, 2:34 AM EDTEdited Apr 19, 2011, 2:36 AM EDT
please tell me how to convert image to square binary matrix in c++ or any other at the earliest thanks [email protected]
venkatPosted Jan 28, 2011, 9:11 PM EST
Hi ... thanks for ur code.The problem here is its taking too much time.How to reduce the time delay. In my case need to send the scanned images to central server.Insted of sending images i am planning to send the binary data to central server through http post method. Is there any other way or sample code which will reduce the time delay at the time of convertion.
bikash karmokarPosted Jan 10, 2011, 11:50 PM EST
thanks for that
Jasmin GandhiPosted Dec 22, 2010, 12:29 AM EST
plz tell me how to get image from sql database in picturebox
hai nam ngoPosted Oct 12, 2010, 5:47 AM EDT
this code is very slow :(
Abid AliPosted Aug 25, 2010, 9:18 PM EDT
Hi Respected Sir! I am working on my FYP and searching for a C# code that can convert scanned image to the Binary digits Kindly help me if possilbe I will be thankful to you With Regards Abid Ali [email protected]
guru devPosted May 10, 2010, 8:16 AM EDT
hi can u help me to convert image egb information into matrix format using c++
Rohit SharmaPosted Feb 9, 2010, 8:54 PM EST
Sir , could you please guide us how to read characters from a image taken by a camera.
rtrow dfkfkfPosted Aug 3, 2009, 8:21 PM EDT
if (img.GetPixel(j, i).A.ToString() == "255" && img.GetPixel(j, i).B.ToString() == "255" && img.GetPixel(j, i).G.ToString() == "255" && img.GetPixel(j, i).R.ToString() == "255")
michael dwyerPosted May 2, 2009, 11:38 AM EDT
this is my site
zeroPosted Apr 21, 2009, 9:15 PM EDT
nice code...
koti kPosted Feb 20, 2009, 6:31 PM EST
sir i am doing stegnography implimentation (Audio) pls send me to mail id [email protected]
ryanPosted Mar 12, 2008, 5:54 AM EDT
does anybody know of a website where this can be done easily online? Thanks Ryan
gava psPosted Feb 27, 2008, 2:06 AM EST
its really a dam good code... it taking just one day to convert a small image to binary.... well done my boy
danieleohPosted Dec 21, 2007, 7:37 PM EST
If this code run, it will give error Exception:"Parameter must be Positive and < Height change this section for (int i = 0; i < img.Width; i++) { for (int j = 0; j < img.Height; j++) to for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) GetPixel(j,i) will give exception otherwise.
shivnath GPosted Dec 10, 2007, 10:51 PM EST
I have a word file which contents a small images one on each line I neeed to read this images and convert them to binary format and have to save this binary data to Database on next page I need to display this images I am not getting how to do all this Please help me in this regard Its very Critical to me
parag wadkarPosted Nov 1, 2007, 2:14 AM EDT
Hello Sir, Your R&D is excellent. It is very good. Just to avoid re-inventing the wheel, can u send me the code to read a char or word from a text image and store in the database? Thanks and Regards, Parag
wei minPosted Oct 30, 2007, 4:13 AM EDT
Hi, This works great. I have a project that requires me to display an image onto a 16x16 resolution display panel. 1)Is there a way to resize the image to 16x16 resolution before converting it? 2)How can I save the result into a bin file? Thanks
sema phorePosted Oct 16, 2007, 10:00 AM EDT
it's very good article thanks, But there is an Exception:"Parameter must be Positive and < Height" Parameter Name: Y So,Can You Solve this for me,plz Thanks Again
kien nguyenPosted Sep 13, 2007, 9:28 PM EDT
Sir, I have an Image that has some charactors. I want to reade numer from that Image pls send me the source code in C#
mohammed essamPosted Jul 17, 2007, 7:07 AM EDT
Realy It's very Nice
Vishwanath BPosted Jun 21, 2007, 8:31 PM EDT
Its ok
subrat mahantyPosted May 15, 2007, 8:50 AM EDT
hello sir can u send me the code for how to read a char or word from a text image
nethal azwaaPosted Mar 23, 2007, 10:46 AM EDT
realy very very good thanks
binu M APosted Mar 9, 2007, 8:17 PM EST
Sir, I have an Image that has some charactors. I want to reade these charactors from that Image pls send me the source code in vb.net