Convert Image To Binary

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

  1. Open the visual studio, create a new form, windows application. 
  2. Drag the follow items to the form: Two Buttons, One pictureBox, One TextBox and one OpenDialog. 
  3. Make the TextBox Multiline on the properties page. 
  4. 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:

  1. openFileDialog1.FileName = ""// Setting the opefiledialog to default    
  2. openFileDialog1.Title = "Images"// Title of the dialog    
  3. openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png"// In the filter you should write all the types of image   
  4. // that you would like to open separete by |, example: "Png Images| *.png"    
  5. openFileDialog1.ShowDialog(); // This show the dialog   
  6. if (openFileDialog1.FileName.ToString() != ""// we check that theres a file selected.  
  7. {  
  8.     Imagen.ImageLocation = openFileDialog1.FileName.ToString(); //Setting the pictureBox.Imagelocation to the  
  9.     direction.  
  10. }  

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:

  1. Bitmap img;  

This one let us work with the image withou modify the original. Then you should add some code after the,

  1. "Imagen.ImageLocation = openFileDialog1.FileName.ToString();"  
  2. 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:

  1. // First we declare the variable that will make the strig that will be printed in the textbox    
  2. string texto = "";  
  3. //The we make the cicles to read pixel by pixel and we make the comparation with the withe color.    
  4. for (int i = 0; i < img.Height; i++) {  
  5.     for (int j = 0; j < img.Width; j++) {  
  6.         //When we add the value to the string we should invert the  
  7.         order because the images are reading from top to bottom  
  8.         //and the textBox is write from left to right.    
  9.         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") {  
  10.             texto = texto + "0";  
  11.         } else {  
  12.             texto = texto + "1";  
  13.         }  
  14.     }  
  15.     texto = texto + "\r\n"// this is to make the enter between lines    
  16. }  
  17. //And finally we put the string to the textbox    
  18. 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.


Similar Articles