Convert Text to Image Using C#

We can easily convert text to image in C# using System.Drawing Namespace. Here this article will guide you how to create a bitmap image from text. it’s using Graphics property of System.Drawing namespace to convert Text to Image. The Graphics class provides methods for drawing to display device.

In this article, you will see a method Named ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int height)  is responsible to return bitmap image on the basis of Text, fontname and fontsize, backcolor and forecolor with apporpriate width and height.

Take a look how to create this function

class ImageUtils
    {
        public static Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
        {
            Bitmap bmp = new Bitmap(width, Height);
            using (Graphics graphics = Graphics.FromImage(bmp))
            {

                Font font = new Font(fontname, fontsize);
                graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
                graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
                graphics.Flush();
            }
            return bmp;
        }
    }

you can use given method in your program after just adding class Imageutils, take a look how

<pre> private void btnconvert_Click(object sender, EventArgs e)
        {
            // Calling ConvertTextToImage function
            picbox.Image = ImageUtils.ConvertTextToImage(textBox1.Text, "Bookman Old Style", 10,
                Color.Yellow, Color.Red, textBox1.Width, textBox1.Height);
        }

Take a look how it call in project

How to Call Function in your Project

Take a look after using function and giving appropriate parameter how it look like after compiling
How it looks after Compiling

Now Download Sourcecode for more details and keep enjoy with changing text to  image.
ClickMe  to Download SourceCode.  🙂

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C#, Tips&Tricks and tagged , , . Bookmark the permalink.

3 Responses to Convert Text to Image Using C#

  1. Mahesh says:

    can you please send the code for image to text converter
    Thanks
    Mahesh

    • Sorry Mahesh! you have to work by yourself. In case of any query or confusion you can revert back. Have some hint – you can convert image to Memory Stream and then from Memory Stream to Base64String.

  2. Coruscate says:

    i can not download means give me error like Not Found

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s