Monday, October 11, 2010

How to Convert Image to Binary String?

Here you will learn How Convert Image to Binary String using of C# and the .NET Framework.

Basically, the program "reads" the image pixel by pixel, and where there is a pixel that is something else than white it will Append 1, else 0 in the string.

private string ConvertoToString(string imagePath)
{
string text = "";
System.Drawing.Bitmap bitMap = new Bitmap(imagePath);
for (int i = 0; i < bitMap.Height; i++)
{
for (int j = 0; j < bitMap.Width; j++)
{
if (bitMap.GetPixel(j, i).A.ToString() == "255" && bitMap.GetPixel(j, i).B.ToString() == "255" && bitMap.GetPixel(j, i).G.ToString() == "255" && bitMap.GetPixel(j, i).R.ToString() == "255")
{
text = text + "0";
}
else
{
text = text + "1";
}
}
text = text + "";
}
return ( text);
}

No comments: