1: private int _height = 75;
2: public int height
3: {
4: get { return _height; }
5: set { _height = value; }
6: }
7:
8: private string _text = "generateimage";
9: public string text
10: {
11: get { return _text; }
12: set { _text = value; }
13: }
14:
15: private int _width = 275;
16: public int width
17: {
18: get { return _width; }
19: set { _width = value; }
20: }
21: /// <summary>
22: /// Get Character from Random Number
23: /// </summary>
24: /// <param name="genNo"></param>
25: /// <returns></returns>
26: public string getChar(int genNo)
27: {
28: switch (genNo)
29: {
30: case 0:
31: return "a";
32: case 1:
33: return "b";
34: case 2:
35: return "c";
36: case 3:
37: return "d";
38: case 4:
39: return "e";
40: case 5:
41: return "f";
42: case 6:
43: return "g";
44: case 7:
45: return "h";
46: case 8:
47: return "i";
48: case 9:
49: return "j";
50: }
51: return string.Empty;
52: }
53:
54: //generating random numbers.
55: private Random ObjRandom = new Random();
56:
57: protected void Page_Load(object sender, EventArgs e)
58: {
59: //generate random text
60: Random r = new Random();
61: string str =r.Next().ToString().Substring(0,4);
62: char[] ch = str.ToCharArray();
63: text = string.Empty;
64: foreach (char var in ch)
65: {
66: //get appropriate char
67: text += getChar(Convert.ToInt32(var.ToString()));
68: }
69: //generate random image
70: GenerateImage();
71: }
72: /// <summary>
73: /// Generate Image
74: /// </summary>
75: private void GenerateImage()
76: {
77: //using System.Drawing; and using System.Drawing.Imaging;
78: //Create a new 32-bit bitmap image.
79: //specify height width
80: //if you want to change pass that value in to query string
81: Bitmap ObjBitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);
82: //Create a graphics object
83: Graphics ObjGraphic = Graphics.FromImage(ObjBitmap);
84: ObjGraphic.SmoothingMode = SmoothingMode.HighQuality;
85:
86: Rectangle ObjRect = new Rectangle(0, 0, this.width, this.height);
87:
88: // Fill in the background color
89: //using System.Drawing.Drawing2D;
90: //you specify different fillup style
91: HatchBrush ObjHatchBrush = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Transparent, Color.Transparent);
92: ObjGraphic.FillRectangle(ObjHatchBrush, ObjRect);
93: // Text Font Size
94: SizeF ObjectFontSize;
95: float fontSize = ObjRect.Height + 3;
96: Font ObjFont;
97: // Adjust the font size until the text fits within the image.
98: do
99: {
100: fontSize--;
101: ObjFont = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Bold);
102: ObjectFontSize = ObjGraphic.MeasureString(this.text, ObjFont);
103: } while (ObjectFontSize.Width > ObjRect.Width);
104:
105: // Set up the text format.
106: StringFormat ObjectStringFormat = new StringFormat();
107: ObjectStringFormat.Alignment = StringAlignment.Center;
108: ObjectStringFormat.LineAlignment = StringAlignment.Center;
109:
110: // Create a path using the text and warp it randomly.
111: GraphicsPath ObjGraphicPath = new GraphicsPath();
112: ObjGraphicPath.AddString(this.text, ObjFont.FontFamily, (int)ObjFont.Style, ObjFont.Size, ObjRect, ObjectStringFormat);
113: float size = 6F;
114: PointF[] points =
115: {
116: new PointF(this.ObjRandom.Next(ObjRect.Width) / size, this.ObjRandom.Next(ObjRect.Height) / size),
117: new PointF(ObjRect.Width - this.ObjRandom.Next(ObjRect.Width) / size, this.ObjRandom.Next(ObjRect.Height) / size),
118: new PointF(this.ObjRandom.Next(ObjRect.Width) / size, ObjRect.Height - this.ObjRandom.Next(ObjRect.Height) / size),
119: new PointF(ObjRect.Width - this.ObjRandom.Next(ObjRect.Width) / size, ObjRect.Height - this.ObjRandom.Next(ObjRect.Height) / size)
120: };
121: Matrix ObjMatrix = new Matrix();
122: ObjMatrix.Translate(0F, 0F);
123: ObjGraphicPath.Warp(points, ObjRect, ObjMatrix, WarpMode.Perspective, 0F);
124:
125: //Draw Text
126: ObjHatchBrush = new HatchBrush(HatchStyle.Wave, Color.Gray, Color.DarkGray);
127: ObjGraphic.FillPath(ObjHatchBrush, ObjGraphicPath);
128:
129: //Add more noise in the image
130: int m = Math.Max(ObjRect.Width, ObjRect.Height);
131: for (int i = 0; i < (int)(ObjRect.Width * ObjRect.Height / 30F); i++)
132: {
133: int x = this.ObjRandom.Next(ObjRect.Width);
134: int y = this.ObjRandom.Next(ObjRect.Height);
135: int w = this.ObjRandom.Next(m / 52);
136: int h = this.ObjRandom.Next(m / 52);
137: ObjGraphic.FillEllipse(ObjHatchBrush, x, y, w, h);
138: }
139: ObjFont.Dispose();
140: ObjHatchBrush.Dispose();
141: ObjGraphic.Dispose();
142: this.Response.ContentType = "image/jpeg";
143: Session.Add("ImageString",this.text);
144: ObjBitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);
145: }