65.9K
CodeProject is changing. Read more.
Home

Generating Random Image Text

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.33/5 (3 votes)

Sep 8, 2010

CPOL
viewsIcon

14988

One method to generate random text

In this article, I will explain one method to generate random text and explore it in image, text color, style and spaces between letters and symbols chosen randomly.
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
    Random rnd = new Random();   
    protected void Page_Load(object sender, EventArgs e)
    {
        Generate_RandomImage();
    }
    void Generate_RandomImage()
    {
        Bitmap img = new Bitmap(400, 50);
        Graphics gr = Graphics.FromImage(img);
        string strSource = "The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae) and is a perennial.";
        List<char> lstChars = strSource.ToList<char>();
        int cntChars = 10;
        List<char> tmp = (lstChars.ToArray<char>()).ToList<char>();
        lstChars.Clear();
        for (int c = 1; c <= cntChars; c++)
        {
        next:
            int randIndex = rnd.Next(0, tmp.Count - 1);
            char chr = tmp[randIndex];
            if (chr == ' ')
            {
                goto next;
            }
            else
            {
                lstChars.Add(chr);
            }
        }
        Font font = new Font("Arial", 20, Random_FontStyle());
        System.Text.StringBuilder txt = new System.Text.StringBuilder();
        foreach (char item in lstChars)
        {
            txt.Append(item.ToString() + Generate_Spaces());
        }
        gr.DrawString(txt.ToString(), font, Random_Color(), 0, 0);
        //img.Save(System.IO.Path.Combine(Request.PhysicalApplicationPath, "img1.png"), System.Drawing.Imaging.ImageFormat.Png);
        Response.ContentType = "image/png";
        img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
        gr.Dispose();
        img.Dispose();
       // RandomImage.ImageUrl = "~/img1.png";
        
    }
    string Generate_Spaces()
    {
        int cnt = rnd.Next(0, 2);
        StringBuilder sb = new StringBuilder();
        for (int c = 0; c <= cnt; c++)
        {
            sb.Append(" ");
        }
        return sb.ToString();
    }
    FontStyle Random_FontStyle()
    {
        int rndStyle = rnd.Next(1, 4);
        FontStyle style = FontStyle.Italic;
        switch (rndStyle)
        {
            case 1:
                style = FontStyle.Bold;
                break;
            case 2:
                style = FontStyle.Italic;
                break;
            case 3:
                style = FontStyle.Regular;
                break;
            case 4:
                style = FontStyle.Strikeout;
                break;
            case 5:
                style = FontStyle.Underline;
                break;
        }
        return style;
    }
    Brush Random_Color()
    {
        Brush col =  Brushes.Black;
        int randCol = rnd.Next(1, 5);
        switch (randCol)
        {
            case 1:
                col = Brushes.Red;
                break;
            case 2:
                col = Brushes.Green;
                break;
            case 3:
                col = Brushes.Red;
                break;
            case 4:
                col = Brushes.Orange;
                break;
            case 5:
                col = Brushes.Black;
                break;
        }
        return col;
    }
}