Click here to Skip to main content
15,886,026 members
Articles / Web Development / ASP.NET
Tip/Trick

Generating Random Image Text

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
18 Sep 2010CPOL 14.3K   3   3
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;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Jordan Jordan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 3 fixing vote Pin
Luc Pattyn12-Sep-10 6:46
sitebuilderLuc Pattyn12-Sep-10 6:46 
GeneralReason for my vote of 3 bla Pin
Moak11-Sep-10 23:35
Moak11-Sep-10 23:35 
GeneralTrying to vote 3 Pin
Luc Pattyn11-Sep-10 11:15
sitebuilderLuc Pattyn11-Sep-10 11:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.