1using System; 2using System.Drawing; 3using System.Drawing.Drawing2D; 4using System.Drawing.Imaging; 5using System.Drawing.Text; 6 7namespace Captcha 8{ 9 public class CaptchaImage 10 { 11 #region Public Properties ... 12 13 public Bitmap Bitmap 14 { 15 get { return mobjBitmap; } 16 } 17 #endregion 18 #region Private vars ... 19 20 private string mstrText; 21 private int mintWidth = 200; 22 private int mintHeight = 50; 23 private string mstrFont = System.Drawing.FontFamily.GenericSerif.Name; 24 private Bitmap mobjBitmap; 25 private Random mobjRandom = new Random(); 26 #endregion 27 #region Constr/Deconstr ... 28 29 public CaptchaImage(string strText, int intWidth, int intHeight) 30 { 31 mstrText = strText; 32 mintWidth = intWidth; 33 mintHeight = intHeight; 34 CreateCaptcha(); 35 } 36 37 public CaptchaImage(string strText, string strFont, int intWidth, int intHeight) 38 { 39 mstrText = strText; 40 mintWidth = intWidth; 41 mintHeight = intHeight; 42 Font objFont = null; 43 try 44 { 45 objFont = new Font(strFont, 12F); 46 mstrFont = strFont; 47 objFont.Dispose(); 48 } 49 catch 50 {} 51 finally 52 { 53 objFont = null; 54 } 55 CreateCaptcha(); 56 } 57 ~CaptchaImage() 58 { 59 Dispose(false); 60 } 61 public void Dispose() 62 { 63 GC.SuppressFinalize(this); 64 Dispose(true); 65 } 66 protected virtual void Dispose(bool disposing) 67 { 68 if (disposing) 69 mobjBitmap.Dispose(); 70 } 71 #endregion 72 #region CreateCaptcha ... 73 74 private void CreateCaptcha() 75 { 76 //Create instance of bitmap object 77 Bitmap objBitmap = new Bitmap(mintWidth, mintHeight, PixelFormat.Format32bppArgb); 78 79 //Create instance of graphics object 80 Graphics objGraphics = Graphics.FromImage(objBitmap); 81 objGraphics.SmoothingMode = SmoothingMode.AntiAlias; 82 Rectangle objRect = new Rectangle(0, 0, mintWidth, mintHeight); 83 84 //Fill the background in a light gray pattern 85 HatchBrush objHatchBrush = new HatchBrush(HatchStyle.DiagonalCross, Color.LightGray, Color.White); 86 objGraphics.FillRectangle(objHatchBrush, objRect); 87 88 //Determine the appropriate font size 89 SizeF objSize; 90 float flFontSize = objRect.Height + 1; 91 Font objFont; 92 do //Decrease font size until text fits within the space 93 { 94 flFontSize--; 95 objFont = new Font(mstrFont, flFontSize, FontStyle.Bold); 96 objSize = objGraphics.MeasureString(mstrText, objFont); 97 } while (objSize.Width > objRect.Width); 98 99 //Format the text 100 StringFormat objStringFormat = new StringFormat(); 101 objStringFormat.Alignment = StringAlignment.Center; 102 objStringFormat.LineAlignment = StringAlignment.Center; 103 104 //Create a path using the text and randomly warp it 105 GraphicsPath objGraphicsPath = new GraphicsPath(); 106 objGraphicsPath.AddString(mstrText, objFont.FontFamily, (int)objFont.Style, objFont.Size, objRect, objStringFormat); 107 float flV = 4F; 108 109 //Create a parallelogram for the text to draw into 110 PointF[] arrPoints = 111 { 112 new PointF(mobjRandom.Next(objRect.Width) / flV, mobjRandom.Next(objRect.Height) / flV), 113 new PointF(objRect.Width - mobjRandom.Next(objRect.Width) / flV, mobjRandom.Next(objRect.Height) / flV), 114 new PointF(mobjRandom.Next(objRect.Width) / flV, objRect.Height - mobjRandom.Next(objRect.Height) / flV), 115 new PointF(objRect.Width - mobjRandom.Next(objRect.Width) / flV, objRect.Height - mobjRandom.Next(objRect.Height) / flV) 116 }; 117 118 //Create the warped parallelogram for the text 119 Matrix objMatrix = new Matrix(); 120 objMatrix.Translate(0F, 0F); 121 objGraphicsPath.Warp(arrPoints, objRect, objMatrix, WarpMode.Perspective, 0F); 122 123 //Add the text to the shape 124 objHatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.DarkGray, Color.Black); 125 objGraphics.FillPath(objHatchBrush, objGraphicsPath); 126 127 //Add some random noise 128 int intMax = Math.Max(objRect.Width, objRect.Height); 129 for (int i = 0; i < (int) (objRect.Width * objRect.Height / 30F); i++) 130 { 131 int x = mobjRandom.Next(objRect.Width); 132 int y = mobjRandom.Next(objRect.Height); 133 int w = mobjRandom.Next(intMax / 15); 134 int h = mobjRandom.Next(intMax / 70); 135 objGraphics.FillEllipse(objHatchBrush, x, y, w, h); 136 } 137 138 //Release memory 139 objFont.Dispose(); 140 objHatchBrush.Dispose(); 141 objGraphics.Dispose(); 142 143 //Set the public property to the 144 mobjBitmap = objBitmap; 145 } 146 #endregion 147 } 148}