C#实现的图片复杂验证码

  1. using System; 
  2.  
  3. using System.Collections.Generic; 
  4.  
  5. using System.Drawing; 
  6.  
  7. using System.Drawing.Drawing2D; 
  8.  
  9. using System.Text; 
  10.  
  11.   
  12.  
  13. namespace Smark.ImageNumber 
  14.  
  15.  
  16.     ///  
  17.  
  18.     /// Copyright © henryfan 2012         
  19.  
  20.     /// Email:  henryfan@msn.com     
  21.       
  22.  
  23.     /// CreateTime: 2012/10/23 21:18:29 
  24.  
  25.     ///  
  26.  
  27.     public class Generator 
  28.  
  29.     { 
  30.  
  31.         const string VALUE = "123456789"
  32.  
  33.   
  34.  
  35.         private static Dictionary mCharImages = new Dictionary(26); 
  36.  
  37.   
  38.  
  39.         private static Image mLine; 
  40.  
  41.   
  42.  
  43.         private static Random mRam = new Random(); 
  44.  
  45.   
  46.  
  47.         public static string GeneratorCode() 
  48.  
  49.         { 
  50.  
  51.             string code = ""
  52.  
  53.             for (int i = 0; i < 4; i++) 
  54.  
  55.             { 
  56.  
  57.                 code += VALUE.Substring(GetRamValue() % VALUE.Length, 1); 
  58.  
  59.             } 
  60.  
  61.             return code; 
  62.  
  63.   
  64.  
  65.         } 
  66.  
  67.   
  68.  
  69.         static int GetRamValue() 
  70.  
  71.         { 
  72.  
  73.   
  74.  
  75.             return mRam.Next(); 
  76.  
  77.         } 
  78.  
  79.   
  80.  
  81.         public static Image GetImage(string code) 
  82.  
  83.         { 
  84.  
  85.             Bitmap tmpImg = new Bitmap(100, 40); 
  86.  
  87.             using (Graphics e = Graphics.FromImage(tmpImg)) 
  88.  
  89.             { 
  90.  
  91.   
  92.  
  93.                 int offset = 2; 
  94.  
  95.   
  96.  
  97.                 e.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60); 
  98.  
  99.   
  100.  
  101.                 List items = new List(); 
  102.  
  103.                 foreach (char key in code) 
  104.  
  105.                 { 
  106.  
  107.                     items.Add(GetGeneratorItem(key)); 
  108.  
  109.                 } 
  110.  
  111.                 for (int i = 0; i < items.Count; i++) 
  112.  
  113.                 { 
  114.  
  115.                     if (i > 0) 
  116.  
  117.                     { 
  118.  
  119.                         if ((items[i].Value < 0 && items[i - 1].Value > 0) || (items[i].Value > 0 && items[i - 1].Value > 0) 
  120.  
  121.                             || (items[i].Value < 0 && items[i - 1].Value < 0)) 
  122.  
  123.                             offset += 12; 
  124.  
  125.   
  126.  
  127.                         else 
  128.  
  129.                             offset += 18; 
  130.  
  131.                     } 
  132.  
  133.                     using (Image img = items[i].DrawImage()) 
  134.  
  135.                     { 
  136.  
  137.                         if (Math.Abs(items[i].Value) > 20) 
  138.  
  139.                             e.DrawImage(img, offset, 6); 
  140.  
  141.                         else if (Math.Abs(items[i].Value) > 20) 
  142.  
  143.                             e.DrawImage(img, offset, 4); 
  144.  
  145.                         else 
  146.  
  147.                             e.DrawImage(img, offset, 2); 
  148.  
  149.                     } 
  150.  
  151.   
  152.  
  153.                 } 
  154.  
  155.   
  156.  
  157.   
  158.  
  159.             } 
  160.  
  161.             return tmpImg; 
  162.  
  163.         } 
  164.  
  165.   
  166.  
  167.         public static System.Collections.IEnumerable GetKeys 
  168.  
  169.         { 
  170.  
  171.             get 
  172.  
  173.             { 
  174.  
  175.                 return mCharImages.Keys; 
  176.  
  177.             } 
  178.  
  179.         } 
  180.  
  181.   
  182.  
  183.         private static GeneratorItem GetGeneratorItem(char key) 
  184.  
  185.         { 
  186.  
  187.             GeneratorItem item = new GeneratorItem(); 
  188.  
  189.             int value = GetRamValue() % 25; 
  190.  
  191.             if (value < 10) 
  192.  
  193.                 value = 10; 
  194.  
  195.             if (GetRamValue() % 2 == 0) 
  196.  
  197.                 value = -value; 
  198.  
  199.             item.Value = value; 
  200.  
  201.             item.Key = key; 
  202.  
  203.             return item; 
  204.  
  205.         } 
  206.  
  207.   
  208.  
  209.         class GeneratorItem 
  210.  
  211.         { 
  212.  
  213.             public int Value { getset; } 
  214.  
  215.             public char Key { getset; } 
  216.  
  217.             public Image DrawImage() 
  218.  
  219.             { 
  220.  
  221.                 Bitmap tmpImg = new Bitmap(50, 50); 
  222.  
  223.                 using (Graphics e = Graphics.FromImage(tmpImg)) 
  224.  
  225.                 { 
  226.  
  227.                     e.RotateTransform(Value, System.Drawing.Drawing2D.MatrixOrder.Append); 
  228.  
  229.                     e.DrawImage(mCharImages[Key], 0, 0); 
  230.  
  231.                     e.Flush(); 
  232.  
  233.                 } 
  234.  
  235.                 return tmpImg; 
  236.  
  237.             } 
  238.  
  239.         } 
  240.  
  241.   
  242.  
  243.         static Generator() 
  244.  
  245.         { 
  246.  
  247.   
  248.  
  249.             HatchBrush sb = new HatchBrush(HatchStyle.Percent40, Color.Black, Color.Black); 
  250.  
  251.   
  252.  
  253.             foreach (char item in VALUE) 
  254.  
  255.             { 
  256.  
  257.   
  258.  
  259.                 Bitmap bmp = new Bitmap(50, 50); 
  260.  
  261.                 using (Graphics g = Graphics.FromImage(bmp)) 
  262.  
  263.                 { 
  264.  
  265.                     g.DrawString(new string(new char[] { item }), new Font("宋体", 24, FontStyle.Italic), sb, 2, 2); 
  266.  
  267.                 } 
  268.  
  269.                 mCharImages[item]= bmp; 
  270.  
  271.             } 
  272.  
  273.   
  274.  
  275.             mLine = new Bitmap(60, 4); 
  276.  
  277.             using (Graphics g = Graphics.FromImage(mLine)) 
  278.  
  279.             { 
  280.  
  281.                 g.FillRectangle(sb, 0, 0, 60, 3); 
  282.  
  283.             } 
  284.  
  285.   
  286.  
  287.         } 
  288.  
  289.     } 
  290.  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值