mvc實(shí)現(xiàn)圖片驗(yàn)證碼功能
mvc中實(shí)現(xiàn)圖片驗(yàn)證碼很簡單,只需要?jiǎng)?chuàng)建一個(gè) FileContentResult的方法,返回file就行
/// <summary>
/// 創(chuàng)建一個(gè)文件方法
/// </summary>
/// <returns></returns>
public FileContentResult GetCode() {
//參數(shù)一:產(chǎn)生幾個(gè)字符的驗(yàn)證碼圖片 參數(shù)二:驗(yàn)證碼的形式(數(shù)字、字母、數(shù)字字母混合都有)
ValidateCode validCode = new ValidateCode(5, ValidateCode.CodeType.Alphas);
//將圖片轉(zhuǎn)換為二進(jìn)制
MemoryStream ms =validCode.CreateCheckCodeImage() as MemoryStream;
dateCode = validCode.CheckCode; //通過 CheckCode獲取當(dāng)前的驗(yàn)證碼
byte[] buffurPic = ms.ToArray();
return File(buffurPic, "image/jpeg");
}
以下是生成驗(yàn)證碼代碼:
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
///
/// 驗(yàn)證碼長度
/// 驗(yàn)證碼類型:字母、數(shù)字、字母+ 數(shù)字
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);
// 取得當(dāng)前點(diǎn)的顏色
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);
}
// 畫圖片的前景噪音點(diǎn)
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
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springmvc處理響應(yīng)數(shù)據(jù)的解析
- springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解
- Springmvc的運(yùn)行流程圖文詳解
- Spring整合Springmvc的相關(guān)介紹
- 詳解Spring MVC/Boot 統(tǒng)一異常處理最佳實(shí)踐
- C#語言MVC框架Aspose.Cells控件導(dǎo)出Excel表數(shù)據(jù)
- Spring mvc防止數(shù)據(jù)重復(fù)提交的方法
- Spring MVC處理參數(shù)中的枚舉類型通用實(shí)現(xiàn)方法
- spring+springmvc+mybatis 開發(fā)JAVA單體應(yīng)用
- MVC設(shè)定默認(rèn)路由為指定的Area下的某個(gè)action
相關(guān)文章
asp.net防止刷新時(shí)重復(fù)提交(可禁用工具條刷新按鈕)
前段時(shí)間遇到了需要禁用刷新的需求,f5按鈕就不說了,簡單的js就能把它禁用,但是工具條上的刷新按鈕卻防止不了啊,不過本文介紹的一種方法卻可以解決此問題,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助2013-01-01
使CheckBoxList的Attributes屬性生效(修改微軟的一個(gè)bug)
使CheckBoxList的Attributes屬性生效(修改微軟的一個(gè)bug)...2007-08-08
asp.net GridView 刪除時(shí)彈出確認(rèn)對(duì)話框(包括內(nèi)容提示)
GridView 刪除時(shí)彈出確認(rèn)對(duì)話框(包括內(nèi)容提示)2009-12-12
asp.net中WebResponse 跨域訪問實(shí)例代碼
一篇朋友很久前寫的asp.net中WebResponse 跨域訪問示例,下面我轉(zhuǎn)過來與大家一起學(xué)習(xí)學(xué)習(xí),希望文章對(duì)大家會(huì)有幫助2014-01-01
ASP.NET Core 過濾器中使用依賴注入知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于ASP.NET Core 過濾器中使用依賴注入的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10
asp.net 光棒效應(yīng)實(shí)現(xiàn)代碼
asp.net 光棒效應(yīng)(今天剛剛學(xué)到的)2009-12-12
Asp.net內(nèi)置對(duì)象之Request對(duì)象(概述及應(yīng)用)
Request對(duì)象主要用于獲取來自客戶端的數(shù)據(jù),如用戶填入表單的數(shù)據(jù)、保存在客戶端的Cookie等,本文將圍繞Request對(duì)象,講解其的主要作用:讀取窗體變量、讀取查詢字符串變量、取得Web服務(wù)器端的系統(tǒng)信息。取得客戶端瀏覽器信息等等,感興趣的朋友可以了解下2013-02-02

