Click here to Skip to main content
Click here to Skip to main content

Generating Random Image Text

, 18 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

alrosan
Software Developer
Jordan Jordan
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 3 fixing vote PinmvpLuc Pattyn12-Sep-10 7:46 
GeneralReason for my vote of 3 bla PinmemberMoak12-Sep-10 0:35 
GeneralTrying to vote 3 PinmvpLuc Pattyn11-Sep-10 12:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 18 Sep 2010
Article Copyright 2010 by alrosan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid