SIGN UP MEMBER LOGIN:    
ARTICLE

Convert Image to Binary

Posted by Orlando Gutierrez Articles | GDI+ & Graphics February 16, 2007
In this tutorial you'll learn how to convert an image to a matrix of zeros and ones, aswell as how to open an image in a picture box.
Reader Level:
Download Files:
 

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:

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.Width; i++)

{ 

    for (int j = 0; j < img.Height; 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.

Login to add your contents and source code to this article
share this article :
post comment
 

sir, what about converting a template to binary?

Posted by choey gamboa Mar 24, 2012

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

Posted by chathuri buddhima May 26, 2011

it's very good...

Posted by jitendra madan Apr 27, 2011

please tell me how to convert image to square binary matrix in c++ or any other at the earliest thanks ayushibmiet@gmail.com

Posted by ayushi agg Apr 19, 2011

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.

Posted by venkat Jan 29, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor