Tuesday, August 11, 2009

How To: Convert Image to String Using C#

the easy way to convert any image to string is :

1- Convert Image to Memory Stream.
2- Convert Memory Stream to Base64String.

so in this sample code I will convert the image to string and I'll save this string in text file.

private string ConvertImage(Bitmap sBit)
{
MemoryStream imageStream = new MemoryStream();
sBit.Save(imageStream, ImageFormat.Jpeg);

return Convert.ToBase64String(imageStream.ToArray());
}

private void button1_Click(object sender, EventArgs e)
{
Bitmap sBit = new Bitmap(@"C:\bmw.jpg");
string imageString = ConvertImage(sBit);

StreamWriter sw = new StreamWriter(@"C:\waleedelkot.text", false);
sw.Write(imageString);
sw.Close();
}

5 comments:

  1. dear sir can u work for me
    i can pay u a lot
    my gmail id is uday.binny@gmail.com

    i am working in data processing, i need a developer like u

    ReplyDelete
  2. Hi, How can i get this to a string array?

    ReplyDelete
  3. it's only converting the bits into pexils,

    not charaters or image processing


    please read more about OCR/ICR/OMR etc..

    ReplyDelete
  4. sir i have to prepare one project in .net.. in which i have to convert one jpg image(scan image) into text file.....
    can u tell me the code for this?
    pls help me
    emailid :- pratibha.sharma9@gmail.com

    ReplyDelete
  5. That was really funny, actually more funny than sBit.ToString()

    ReplyDelete