1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Drawing;  
  12. namespace CNTZCMS.ClassLib  
  13. {  
  14.     /// <summary>  
  15.     ///RndCode 的摘要说明  
  16.     /// </summary>  
  17.     public class RndCode  
  18.     {  
  19.         public RndCode()  
  20.         {  
  21.             //  
  22.             //TODO: 在此处添加构造函数逻辑  
  23.             //  
  24.         }  
  25.         public static string GenerateCheckCode()  
  26.         {  
  27.             int intFirst, intSec, intTemp;  
  28.  
  29.             string checkCode = String.Empty;  
  30.  
  31.             System.Random random = new Random();  
  32.  
  33.             intFirst = random.Next(1, 10);  
  34.             intSec = random.Next(1, 10);  
  35.             switch (random.Next(1, 3).ToString())  
  36.             {  
  37.                 case "2":  
  38.                     if (intFirst < intSec)  
  39.                     {  
  40.                         intTemp = intFirst;  
  41.                         intFirst = intSec;  
  42.                         intSec = intTemp;  
  43.                     }  
  44.                     checkCode = intFirst + "-" + intSec + "=";  
  45.                     System.Web.HttpContext.Current.Session["ValidCode"] = intFirst - intSec;  
  46.                     break;  
  47.                 default:  
  48.                     checkCode = intFirst + "+" + intSec + "=";  
  49.                     System.Web.HttpContext.Current.Session["ValidCode"] = intFirst + intSec;  
  50.                     break;  
  51.             }  
  52.  
  53.             //Response.Cookies.Add(new HttpCookie("ValidCode",Movie.Common.AES.EncryptAes(checkCode)));  
  54.             return checkCode;  
  55.         }  
  56.         #region 产生波形滤镜效果  
  57.         private const double PI = 3.1415926535897932384626433832795;  
  58.         private const double PI2 = 6.283185307179586476925286766559;  
  59.         private System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)  
  60.         {  
  61.             System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);  
  62.  
  63.             // 将位图背景填充为白色  
  64.             System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);  
  65.             graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);  
  66.             graph.Dispose();  
  67.  
  68.             double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;  
  69.  
  70.             for (int i = 0; i < destBmp.Width; i++)  
  71.             {  
  72.                 for (int j = 0; j < destBmp.Height; j++)  
  73.                 {  
  74.                     double dx = 0;  
  75.                     dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;  
  76.                     dx += dPhase;  
  77.                     double dy = Math.Sin(dx);  
  78.  
  79.                     // 取得当前点的颜色  
  80.                     int nOldX = 0, nOldY = 0;  
  81.                     nOldX = bXDir ? i + (int)(dy * dMultValue) : i;  
  82.                     nOldY = bXDir ? j : j + (int)(dy * dMultValue);  
  83.  
  84.                     System.Drawing.Color color = srcBmp.GetPixel(i, j);  
  85.                     if (nOldX >= 0 && nOldX < destBmp.Width  
  86.                      && nOldY >= 0 && nOldY < destBmp.Height)  
  87.                     {  
  88.                         destBmp.SetPixel(nOldX, nOldY, color);  
  89.                     }  
  90.                 }  
  91.             }  
  92.  
  93.             return destBmp;  
  94.         }  
  95.         #endregion  
  96.         public static void CreateCheckCodeImage(string checkCode)  
  97.         {  
  98.             if (checkCode == null || checkCode.Trim() == String.Empty)  
  99.                 return;  
  100.  
  101.             System.Drawing.Bitmap p_w_picpath = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 15.0)), 25);  
  102.             Graphics g = Graphics.FromImage(p_w_picpath);  
  103.  
  104.             try 
  105.             {  
  106.                 //生成随机生成器  
  107.                 Random random = new Random();  
  108.  
  109.                 //清空图片背景色  
  110.                 g.Clear(Color.White);  
  111.  
  112.                 //画图片的背景噪音线  
  113.                 for (int i = 0; i < 12; i++)  
  114.                 {  
  115.                     int x1 = random.Next(p_w_picpath.Width);  
  116.                     int x2 = random.Next(p_w_picpath.Width);  
  117.                     int y1 = random.Next(p_w_picpath.Height);  
  118.                     int y2 = random.Next(p_w_picpath.Height);  
  119.  
  120.                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);  
  121.                 }  
  122.  
  123.                 Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));  
  124.                 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, p_w_picpath.Width, p_w_picpath.Height), Color.Blue, Color.DarkRed, 1.2f, true);  
  125.                 g.DrawString(checkCode, font, brush, 1, 1);  
  126.  
  127.                 //画图片的前景噪音点  
  128.                 //for (int i = 0; i < 100; i++)  
  129.                 //{  
  130.                 //    int x = random.Next(p_w_picpath.Width);  
  131.                 //    int y = random.Next(p_w_picpath.Height);  
  132.  
  133.                 //    p_w_picpath.SetPixel(x, y, Color.FromArgb(random.Next()));  
  134.                 //}  
  135.  
  136.                 //p_w_picpath = TwistImage(p_w_picpath, true, 3, 1);//画图片的波形滤镜效果  
  137.                 //画图片的边框线  
  138.                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, p_w_picpath.Width - 1, p_w_picpath.Height - 1);  
  139.  
  140.                 System.IO.MemoryStream ms = new System.IO.MemoryStream();  
  141.                 p_w_picpath.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);  
  142.                 System.Web.HttpContext.Current.Response.ClearContent();  
  143.                 System.Web.HttpContext.Current.Response.ContentType = "p_w_picpath/Gif";  
  144.                 System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());  
  145.             }  
  146.             finally 
  147.             {  
  148.                 g.Dispose();  
  149.                 p_w_picpath.Dispose();  
  150.             }  
  151.  
  152.         }  
  153.     }  

使用方法:

 CNTZCMS.ClassLib.RndCode.CreateCheckCodeImage(CNTZCMS.ClassLib.RndCode.GenerateCheckCode());