Sunday, February 3, 2008

How to Convert String to Image in C#

I had worked on a project that required a conversion from string the user would enter to an image, this can be used as easy Captcha too. Here is a method of how to Convert String to Image:

public static void DrawText(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int height, int width, string imagePath)
{
    Bitmap img = new Bitmap(height, width);
    Graphics Gimg = Graphics.FromImage(img);
    Font imgFont = new Font(fontName, fontSize);
    PointF imgPoint = new PointF(5, 5);
    SolidBrush bForeColor = new SolidBrush(foreColor);
    SolidBrush bBackColor = new SolidBrush(backColor);

    Gimg.FillRectangle(bBackColor, 0, 0, width, height);
    Gimg.DrawString(txt, imgFont, bForeColor, imgPoint);
    img.Save(imagePath, ImageFormat.Jpeg);
}

Source: http://www.nilebits.com/blog/2007/02/write-text-to-an-image-using-csharp/

Hire Me

Follow me on Facebook

Follow me

Do you find this Blog helpful?

Follow by Email

About Me

My Photo
Expert Senior Software Developer

Microsoft Business Card