TG.WXCRM.V4/WebHelper/SnCodeHelper.cs

151 lines
6.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
namespace WX.CRM.WebHelper
{
public class SnCodeHelper
{
protected static int VCODE_LENGTH = 4;
//产生图片 宽度_WIDTH, 高度_HEIGHT
private static readonly int _WIDTH = 100, _HEIGHT = 35;
//字体集
private static readonly string[] _FONT_FAMIly = { /*"Broadway",*/ "Arial", "Arial Black", "Courier New", "Showcard Gothic" };
//字体大小集
private static readonly int[] _FONT_SIZE = { 16, 17, 18 };
//前景字体颜色集
private static readonly Color[] _COLOR_FACE = { Color.FromArgb(113, 153, 67), Color.FromArgb(30, 127, 182), Color.FromArgb(101, 44, 29), Color.FromArgb(150, 156, 0) };
//背景颜色集
private static readonly Color[] _COLOR_BACKGROUND = { Color.FromArgb(247, 254, 236), Color.FromArgb(234, 248, 255), Color.FromArgb(244, 250, 246), Color.FromArgb(248, 248, 248) };
//文本布局信息
private static StringFormat _DL_FORMAT = new StringFormat(StringFormatFlags.NoClip);
//左右旋转角度
private static readonly int _ANGLE = 15;
public byte[] CreateValidateImage(string code)
{
_DL_FORMAT.Alignment = StringAlignment.Center;
_DL_FORMAT.LineAlignment = StringAlignment.Center;
long tick = DateTime.Now.Ticks;
Random Rnd = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
using (Bitmap _img = new Bitmap(_WIDTH, _HEIGHT))
{
using (Graphics g = Graphics.FromImage(_img))
{
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Point dot = new Point(18 + 5, 18);
// 定义一个无干扰线区间和一个起始位置
int nor = Rnd.Next(53), rsta = Rnd.Next(130);
// 绘制干扰正弦曲线 M:曲线平折度, D:Y轴常量 V:X轴焦距
int M = Rnd.Next(15) + 5, D = Rnd.Next(20) + 15, V = Rnd.Next(5) + 1;
int ColorIndex = Rnd.Next(4);
float Px_x = 0.0F;
float Px_y = Convert.ToSingle(M * Math.Sin(V * Px_x * Math.PI / 180) + D);
float Py_x, Py_y;
//填充背景
g.Clear(_COLOR_BACKGROUND[Rnd.Next(4)]);
//前景刷子 //背景刷子
using (Brush _BrushFace = new SolidBrush(_COLOR_FACE[ColorIndex]))
{
#region 线
for (int i = 0; i < 131; i++)
{
//初始化y点
Py_x = Px_x + 1;
Py_y = Convert.ToSingle(M * Math.Sin(V * Py_x * Math.PI / 180) + D);
//确定线条颜色
if (rsta >= i || i > (rsta + nor))
//初始化画笔
using (Pen _pen = new Pen(_BrushFace, 1.5f))
{
//绘制线条
g.DrawLine(_pen, Px_x, Px_y, Py_x, Py_y);
}
//交替x,y坐标点
Px_x = Py_x;
Px_y = Py_y;
}
#endregion
//初始化光标的开始位置
g.TranslateTransform(-5, 2);
#region
for (int i = 0; i < code.Length; i++)
{
//随机旋转 角度
int angle = Rnd.Next(-_ANGLE, _ANGLE);
//移动光标到指定位置
g.TranslateTransform(dot.X, dot.Y);
//旋转
g.RotateTransform(angle);
//初始化字体
using (Font _font = new Font(_FONT_FAMIly[Rnd.Next(0, _FONT_FAMIly.Count())], _FONT_SIZE[Rnd.Next(0, 3)]))
{
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, _WIDTH, _HEIGHT), _COLOR_FACE[ColorIndex], Color.Orange, 1.2f, true);
//绘制
g.DrawString(code[i].ToString(), _font, brush, 1, 1, _DL_FORMAT);
}
//反转
g.RotateTransform(-angle);
//重新定位光标位置
g.TranslateTransform(-2, -dot.Y);
}
#endregion
}
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
_img.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
/// <summary>
/// 获取随机数
/// </summary>
/// <param name="codeCount"></param>
/// <returns></returns>
public string CreateRandomSatl(int codeCount)
{
string allChar = "2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(allCharArray.Count());
//if (temp == t)
//{
// return CreateRandomSatl(codeCount);
//}
//temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
}
}