当前位置: 移动技术网 > IT编程>开发语言>.net > mvc实现图片验证码功能

mvc实现图片验证码功能

2019年01月07日  | 移动技术网IT编程  | 我要评论

mvc中实现图片验证码很简单,只需要创建一个 filecontentresult的方法,返回file就行

/// <summary>
    /// 创建一个文件方法
    /// </summary>
    /// <returns></returns>
    public filecontentresult getcode() {
      //参数一:产生几个字符的验证码图片 参数二:验证码的形式(数字、字母、数字字母混合都有)
      validatecode validcode = new validatecode(5, validatecode.codetype.alphas);
        //将图片转换为二进制
      memorystream ms =validcode.createcheckcodeimage() as memorystream;
        datecode = validcode.checkcode; //通过 checkcode获取当前的验证码
      byte[] buffurpic = ms.toarray(); 
      return file(buffurpic, "image/jpeg"); 
  
    }

以下是生成验证码代码:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
 
using system.drawing;
using system.drawing.drawing2d;
using system.io;
 
namespace helper
{
  public class validatecode
  {
 
 
   #region private fields 
   private const double pi = 3.1415926535897932384626433832795; 
   private const double pi2 = 6.283185307179586476925286766559; 
   //private readonly int _wordslen = 4; 
   private int _len;
   private codetype _codetype;
   private readonly single _jianju = (float)18.0; 
   private readonly single _height = (float)24.0; 
   private string _checkcode; 
   #endregion 
   #region public property 
   public string checkcode
   {
 
     get
     {
 
       return _checkcode;
 
     }
 
   }
 
   #endregion
 
   #region constructors 
   ///  
   /// public constructors 
   /// 
   /// 验证码长度  
   /// 验证码类型:字母、数字、字母+ 数字 
 
   public validatecode(int len, codetype ctype)
   { 
     this._len = len; 
     this._codetype = ctype;
 
   }
 
   #endregion 
   #region public field 
   public enum codetype { words, numbers, characters, alphas } 
   #endregion 
   #region private methods
 
   public string generatenumbers()
   {
 
     string strout = ""; 
     system.random random = new random();
     for (int i = 0; i < _len; i++)
     {
       string num = convert.tostring(random.next(10000) % 10); 
       strout += num;
 
     }
 
     return strout.trim();
 
   }
 
 
 
   public string generatecharacters()
   {
 
     string strout = ""; 
     system.random random = new random(); 
     for (int i = 0; i < _len; i++)
     {
       string num = convert.tostring((char)(65 + random.next(10000) % 26)); 
       strout += num;
 
     }
 
     return strout.trim(); 
   }
 
   // 
 
   public string generatealphas()
   {
     string strout = ""; 
     string num = ""; 
     system.random random = new random();
     for (int i = 0; i < _len; i++)
     {
 
       if (random.next(500) % 2 == 0)
       {
          num = convert.tostring(random.next(10000) % 10);
 
       }
 
       else
       { 
         num = convert.tostring((char)(65 + random.next(10000) % 26));
 
       }
 
       strout += num;
 
     }
      return strout.trim();
 
   }
 
 
 
   private system.drawing.bitmap twistimage(bitmap srcbmp, bool bxdir, double dmultvalue, double dphase)
   {
      system.drawing.bitmap destbmp = new bitmap(srcbmp.width, srcbmp.height);
 
     // 将位图背景填充为白色 
     system.drawing.graphics graph = system.drawing.graphics.fromimage(destbmp); 
     graph.fillrectangle(new solidbrush(system.drawing.color.white), 0, 0, destbmp.width, destbmp.height); 
     graph.dispose(); 
 
     double dbaseaxislen = bxdir ? (double)destbmp.height : (double)destbmp.width;
 
     for (int i = 0; i < destbmp.width; i++)
     { 
       for (int j = 0; j < destbmp.height; j++)
       { 
         double dx = 0;
         dx = bxdir ? (pi2 * (double)j) / dbaseaxislen : (pi2 * (double)i) / dbaseaxislen;
         dx += dphase; 
         double dy = math.sin(dx);
 

         // 取得当前点的颜色 
         int noldx = 0, noldy = 0;
         noldx = bxdir ? i + (int)(dy * dmultvalue) : i;
         noldy = bxdir ? j : j + (int)(dy * dmultvalue);
 
         system.drawing.color color = srcbmp.getpixel(i, j);
         if (noldx >= 0 && noldx < destbmp.width 
          && noldy >= 0 && noldy < destbmp.height)
         {
 
           destbmp.setpixel(noldx, noldy, color);
 
         }
 
       }
 
     }
 
 
 
     return destbmp;
 
   }
 
   #endregion 
   #region public methods
 
   public stream createcheckcodeimage()
   {
 
     string checkcode; 
     switch (_codetype)
     {
 
       case codetype.alphas:
          checkcode = generatealphas();
         break;
 
       case codetype.numbers:
         checkcode = generatenumbers();
         break;
 
       case codetype.characters: 
         checkcode = generatecharacters(); 
         break; 
       default:
 
         checkcode = generatealphas();
 
         break;
 
     }
 
     this._checkcode = checkcode; 
     memorystream ms = null;
 
     // 
 
     if (checkcode == null || checkcode.trim() == string.empty)
 
       return null;
 
     bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * _jianju)), (int)_height);
     graphics g = graphics.fromimage(image);
 
     try
     {
 
       random random = new random(); 
       g.clear(color.white); 
       // 画图片的背景噪音线 
       for (int i = 0; i < 18; i++)
       {
         int x1 = random.next(image.width); 
         int x2 = random.next(image.width);
         int y1 = random.next(image.height);
         int y2 = random.next(image.height);
 
         g.drawline(new pen(color.fromargb(random.next()), 1), x1, y1, x2, y2);
 
       }
 
       font font = new system.drawing.font("times new roman", 14, system.drawing.fontstyle.bold); 
       lineargradientbrush brush = new lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
 
       if (_codetype != codetype.words)
       {
 
         for (int i = 0; i < checkcode.length; i++)
         {
 
           g.drawstring(checkcode.substring(i, 1), font, brush, 2 + i * _jianju, 1);
 
         }
 
       }
       else
       {
 
         g.drawstring(checkcode, font, brush, 2, 2);
 
       }
 
       // 画图片的前景噪音点 
 
       for (int i = 0; i < 150; i++)
       {
 
         int x = random.next(image.width); 
         int y = random.next(image.height); 
         image.setpixel(x, y, color.fromargb(random.next()));
 
       }
 
       // 画图片的波形滤镜效果 
 
       if (_codetype != codetype.words)
       {
 
         image = twistimage(image, true, 3, 1);
 
       }
 
       // 画图片的边框线 
 
       g.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1); 
       ms = new system.io.memorystream(); 
       image.save(ms, system.drawing.imaging.imageformat.gif);
 
     }
 
     finally
     {
 
       g.dispose(); 
       image.dispose();
 
     }
 
     return ms;
 
   }
 
   #endregion
   
 
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网