以上文章由本文作者根據網路上其它的例子學習整合再加上自己的思想凝聚而成,如有侵犯請聯繫本人,速刪 ...
調用:
public ActionResult Vcode()//驗證碼
{
string code = ValidateCode.CreateRandomCode(4);
ValidateCode.CreateImage(code);
Session["vcode"] = code.ToLower();//存入session供驗證用
System.IO.MemoryStream ms = new System.IO.MemoryStream();
return File(ms.GetBuffer(), "image/JPEG");
}
//下麵是主題class內部
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Web;
namespace xxxxxx.Models//命名空間自己填咯
{
public class ValidateCode
{
public static string CreateRandomCode(int length)
{
int rand;
char code;
string randomcode = String.Empty;
//生成一定長度的驗證碼
System.Random random = new Random();
for (int i = 0; i < length; i++)
{
rand = random.Next();
if (rand % 3 == 0)
{
code = (char)('A' + (char)(rand % 26));//如果是3的倍數就除以26將餘數(小於26)轉換成大寫字母
}
else if (rand % 7 == 0)
{
code = (char)('a' + (char)(rand % 26));
}
else
{
code = (char)('0' + (char)(rand % 10));
}
randomcode += code.ToString();
}
return randomcode;
}
public static void CreateImage(string randomcode)
{
int randAngle = 30; //隨機轉動角度範圍
int mapwidth = (int)(randomcode.Length * 23);//背景的寬度,此值是你輸入要求的字元長度的23倍大(4*23=92寬)
using (Bitmap map = new Bitmap(mapwidth, 28))//創建圖片背景,寬為(4*28),高為28的畫布,從右上角(0,0)處開始;
{
Random rand = new Random();//隨機數實例
using (Bitmap temp = new Bitmap(map.Width, map.Height))//臨時畫布
{
using (Graphics tempGra = Graphics.FromImage(temp))
{
char[] chars = randomcode.ToCharArray();//拆散字元串成單字元數組
StringFormat format = new StringFormat(StringFormatFlags.NoClip);//定義文字格式
format.Alignment = StringAlignment.Center;//文字距中
format.LineAlignment = StringAlignment.Center;
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //定義顏色
string[] fontList = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };//定義字體
for (int i = 0; i < chars.Length; i++)//逐個繪製文字
{
int cindex = rand.Next(7);//顏色隨機數
int findex = rand.Next(5);//字體隨機數
Font font = new System.Drawing.Font(fontList[findex], 13, System.Drawing.FontStyle.Bold);//字體樣式(參數2為字體大小)
Brush brush = new System.Drawing.SolidBrush(c[cindex]);//文字顏色1
//Brush brushPen = new LinearGradientBrush(new Rectangle(0, 0, temp.Width, temp.Height), Color.FromArgb(rand.Next(0, 256), 0, 0), Color.FromArgb(0, 0, rand.Next(0, 256)), rand.Next(90));//文字顏色2漸變
Point dot = new Point(16, 14);//定義一個點
//graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150);//測試X坐標顯示間距的
float angle = rand.Next(-randAngle, randAngle);//轉動的度數
tempGra.TranslateTransform(dot.X, dot.Y);//更改坐標系的原點(所有的操作在此原點(16,16))
tempGra.RotateTransform(angle);//旋轉畫布
//Matrix m = new Matrix();//創建變換
//m.RotateAt(angle, new PointF(16, 16), MatrixOrder.Append);//旋轉
//m.Shear(rand.Next(-10, 10) * 0.03f, 0);//扭曲
//tempGra.Transform = m;//畫布應用變換
tempGra.DrawString(chars[i].ToString(), font, brush, 1, 1, format);//開始畫字母或文字
tempGra.RotateTransform(-angle);//轉回去
tempGra.TranslateTransform(4, -dot.Y);//更改坐標系的原點
}
}
using (Graphics graph = Graphics.FromImage(map))//創建目標畫布;
{
graph.Clear(Color.AliceBlue);//清除畫面,填充背景
Rectangle rect = new Rectangle(0, 0, map.Width, map.Height);//繪製漸變背景
Brush brushBack = new LinearGradientBrush(rect, Color.FromArgb(rand.Next(150, 256), 255, 255), Color.FromArgb(255, rand.Next(150, 256), 255), rand.Next(90));//漸變背景顏色
graph.FillRectangle(brushBack, rect);//背景填充到畫布
graph.DrawRectangle(new Pen(Color.LightPink, 0), 0, 0, map.Width - 1, map.Height - 1);//畫一個邊框,預設是從屏幕右上角(0,0)初開始畫
//graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//圖片抗鋸齒模式
Pen blackPen = new Pen(Color.LightGray, 0);//筆觸顏色
for (int i = 0; i < 50; i++)//噪點50個
{
int x = rand.Next(1, map.Width - 3);
int y = rand.Next(1, map.Height - 3);
graph.DrawRectangle(blackPen, x, y, 1, 1);//生成噪點
}
graph.DrawImage(temp, new Point(0, 0));//先畫點後畫圖
Pen pen = new Pen(Color.Gray, 1);//干擾線筆觸
for (int i = 0; i < 2; i++)//繪製兩條幹擾線
{
Point p1 = new Point(0, rand.Next(map.Height));
Point p2 = new Point(rand.Next(map.Width), rand.Next(map.Height));
Point p3 = new Point(rand.Next(map.Width), rand.Next(map.Height));
Point p4 = new Point(map.Width, rand.Next(map.Height));
Point[] p = { p1, p2, p3, p4 };
graph.DrawBeziers(pen, p);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
temp.Dispose();//釋放
graph.Dispose();
map.Dispose();
}
}
}
}
}
}