Converting an Image to a Binary String

Written By: Dragos Nicholas

- 17 Apr 2008 -
















Description: Today we will learn how to convert an image into a 1 and 0 string using C#. Let me show you what I'm talking about.

  1. Converting an Image to a Binary String

GUI

First, create a new project-->Windows Form Application; name it Imageconverter.

Let's build the GUI. Add 2 buttons, a picturebox, a textbox and an openFileDialog.

In the properties explorer set the following properties on textBox1: Multiline = true, Wordwrap = false, Scrollbars = Both, and Font = Courier New.

Name the first button "Image" and the second "Convert".

Form1 should look like this.

Image Converter GUI Screenshot

Code

After you've done the Graphical User Interface, it's time to write (copy-paste) some code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace ImageConverter
{
    public partial class Form1 : Form
    {
        Bitmap img; //initialize the image
 
        public Form1()
        {
            InitializeComponent();
        }

Double click the button1-"Image" on the UI and it will automatically create a method for you. The following code will simply open a image file and it will show up in the picturebox:

        private void Image_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = ""; // Setting the openfiledialog 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 separated by |, example: "Png Images|*.png"*/
            openFileDialog1.ShowDialog(); // This show the dialog 
            if (openFileDialog1.FileName.ToString() != "") /* we check that there's a file selected.*/
            {
                pictureBox1.ImageLocation = openFileDialog1.FileName.ToString(); /*Setting the pictureBox.Imagelocation to the direction.*/
                img = new Bitmap(openFileDialog1.FileName.ToString()); /* Make a copy of the image in the Bitmap variable*/
            }
        }

This will happen when we click button2-"Convert". Basically, the program "reads" the image pixel by pixel, and when here is a pixel that is something else than white it will put 1, else 0 in the textbox.

        private void Convert_Click(object sender, EventArgs e)
        {
            string text = "";
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    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")
                    {
                        text = text + "0";
                    }
                    else
                    {
                        text = text + "1";
                    }
                }
                text = text + "
";
            }
    
            textBox1.Text = text;
        
        }

Of course, you can put any other character else than "0" and "1" (i.e:text=text+"X";).

The Result

What you have to do is simply put togheter the previous codeblocks, one by one, and run the program. The image should not have more than 50 pixels, because it will take a while for the computer to read more than 2500 pixels. I've attached a sample picture to see the results.

Image Converter Results Screenshot

And that is all. Happy converting!