Click here to Skip to main content
15,892,298 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 
It sure would be nice to see an example result; not sure you can add an image to a Tip/Trick though.

I have some comments on your code, such as:

1.
the goto was really not necessary; either use a do..while, or make sure the input string does not contain any spaces (omit them from the string literal, or remove them using string.Replace)

2.
Generate_Spaces can be simplified based on the string constructor that takes a character and a count.

3.
Why do your random functions start their switch cases at 1? and FontStyle.Underline is not going to happen...
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.


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.