C#封装类NET MVC WEBFORMWord格式.docx

上传人:b****7 文档编号:22396314 上传时间:2023-02-03 格式:DOCX 页数:17 大小:18KB
下载 相关 举报
C#封装类NET MVC WEBFORMWord格式.docx_第1页
第1页 / 共17页
C#封装类NET MVC WEBFORMWord格式.docx_第2页
第2页 / 共17页
C#封装类NET MVC WEBFORMWord格式.docx_第3页
第3页 / 共17页
C#封装类NET MVC WEBFORMWord格式.docx_第4页
第4页 / 共17页
C#封装类NET MVC WEBFORMWord格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

C#封装类NET MVC WEBFORMWord格式.docx

《C#封装类NET MVC WEBFORMWord格式.docx》由会员分享,可在线阅读,更多相关《C#封装类NET MVC WEBFORMWord格式.docx(17页珍藏版)》请在冰豆网上搜索。

C#封装类NET MVC WEBFORMWord格式.docx

v.SetRandomAngle=4;

//文字大小

v.SetFontSize=15;

//背景色

//v.SetBackgroundColor

//前景噪点数量

//v.SetForeNoisePointCount=3;

//v.SetFontColor=Color.Red;

//...还有更多设置不介绍了

varquestionList=newDictionary<

string,string>

()

{"

1+1=?

"

"

2"

},

喜羊羊主角叫什么名字?

喜羊羊"

},

【我爱你】中间的那个字?

爱"

};

varquestionItem=v.GetQuestion();

//不赋值为随机验证码例如:

1*2=?

这种

//指定验证文本

//v.SetVerifyCodeText

v.SetVerifyCodeText=questionItem.Key;

Session["

VerifyCode"

]=questionItem.Value;

//输出图片

v.OutputImage(System.Web.HttpContext.Current.Response);

}

  

2、前台设用action或者pageload页面地址,js给src更换url添加随机参数达到刷新验证码的功能

<

imgsrc="

/File/ValidateImg"

/>

3、完整封装类

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Drawing;

usingSystem.Drawing.Drawing2D;

usingSystem.Drawing.Imaging;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Web;

namespaceIdea.Models

{

///<

summary>

///验证码类

/summary>

publicclassVerifyCodeSugar

privateRandomobjRandom=newRandom();

#regionsetting

/////验证码长度

publicintSetLength=4;

///验证码字符串

publicstringSetVerifyCodeText{get;

set;

///是否加入小写字母

publicboolSetAddLowerLetter=false;

///是否加入大写字母

publicboolSetAddUpperLetter=false;

///字体大小

publicintSetFontSize=18;

/////字体颜色

publicColorSetFontColor=Color.Blue;

///字体类型

publicstringSetFontFamily="

Verdana"

;

///背景色

publicColorSetBackgroundColor=Color.AliceBlue;

///前景噪点数量

publicintSetForeNoisePointCount=2;

///随机码的旋转角度

publicintSetRandomAngle=40;

///是否随机字体颜色

publicboolSetIsRandomColor=false;

///图片宽度

privateintSetWith

get

returnthis.SetVerifyCodeText.Length*SetFontSize;

///图片高度

privateintSetHeight

returnConvert.ToInt32((60.0/100)*SetFontSize+SetFontSize);

#endregion

#regionConstructorMethod

publicVerifyCodeSugar()

this.GetVerifyCodeText();

#regionPrivateMethod

///得到验证码字符串

privatevoidGetVerifyCodeText()

//没有外部输入验证码时随机生成

if(String.IsNullOrEmpty(this.SetVerifyCodeText))

StringBuilderobjStringBuilder=newStringBuilder();

//加入数字1-9

for(inti=1;

i<

=9;

i++)

objStringBuilder.Append(i.ToString());

//加入大写字母A-Z,不包括O

if(this.SetAddUpperLetter)

chartemp='

'

for(inti=0;

26;

temp=Convert.ToChar(i+65);

//如果生成的字母不是'

O'

if(!

temp.Equals('

))

objStringBuilder.Append(temp);

//加入小写字母a-z,不包括o

if(this.SetAddLowerLetter)

temp=Convert.ToChar(i+97);

o'

//生成验证码字符串

intindex=0;

SetLength;

index=objRandom.Next(0,objStringBuilder.Length);

this.SetVerifyCodeText+=objStringBuilder[index];

objStringBuilder.Remove(index,1);

///得到验证码图片

privateBitmapGetVerifyCodeImage()

Bitmapresult=null;

//创建绘图

result=newBitmap(SetWith,SetHeight);

using(GraphicsobjGraphics=Graphics.FromImage(result))

objGraphics.SmoothingMode=SmoothingMode.HighQuality;

//清除整个绘图面并以指定背景色填充

objGraphics.Clear(this.SetBackgroundColor);

//创建画笔

using(SolidBrushobjSolidBrush=newSolidBrush(this.SetFontColor))

this.AddForeNoisePoint(result);

this.AddBackgroundNoisePoint(result,objGraphics);

//文字居中

StringFormatobjStringFormat=newStringFormat(StringFormatFlags.NoClip);

objStringFormat.Alignment=StringAlignment.Center;

objStringFormat.LineAlignment=StringAlignment.Center;

//字体样式

FontobjFont=newFont(this.SetFontFamily,objRandom.Next(this.SetFontSize-3,this.SetFontSize),FontStyle.Regular);

//验证码旋转,防止机器识别

char[]chars=this.SetVerifyCodeText.ToCharArray();

chars.Length;

//转动的度数

floatangle=objRandom.Next(-this.SetRandomAngle,this.SetRandomAngle);

objGraphics.TranslateTransform(12,12);

objGraphics.RotateTransform(angle);

objGraphics.DrawString(chars[i].ToString(),objFont,objSolidBrush,-2,2,objStringFormat);

objGraphics.RotateTransform(-angle);

objGraphics.TranslateTransform(2,-12);

returnresult;

///添加前景噪点

paramname="

objBitmap"

>

<

/param>

privatevoidAddForeNoisePoint(BitmapobjBitmap)

objBitmap.Width*this.SetForeNoisePointCount;

objBitmap.SetPixel(objRandom.Next(objBitmap.Width),objRandom.Next(objBitmap.Height),this.SetFontColor);

///添加背景噪点

objGraphics"

privatevoidAddBackgroundNoisePoint(BitmapobjBitmap,GraphicsobjGraphics)

using(PenobjPen=newPen(Color.Azure,0))

objBitmap.Width*2;

objGraphics.DrawRectangle(objPen,objRandom.Next(objBitmap.Width),objRandom.Next(objBitmap.Height),1,1);

///获取随机颜色

returns>

/returns>

privateColorGetRandomColor()

RandomRandomNum_First=newRandom((int)DateTime.Now.Ticks);

//对于C#的随机数,没什么好说的

System.Threading.Thread.Sleep(RandomNum_First.Next(50));

RandomRandomNum_Sencond=newRandom((int)DateTime.Now.Ticks);

//为了在白色背景上显示,尽量生成深色

intint_Red=RandomNum_First.Next(256);

intint_Green=RandomNum_Sencond.Next(256);

intint_Blue=(int_Red+int_Green>

400)?

0:

400-int_Red-int_Green;

int_Blue=(int_Blue>

255)?

255:

int_Blue;

returnColor.FromArgb(int_Red,int_Green,int_Blue);

#regionPublicMethod

///输出验证码图片

objHttpResponse"

Http响应实例<

输出是否成功<

publicboolOutputImage(HttpResponseobjHttpResponse)

boolresult=false;

if(this.SetIsRandomColor)

this.SetFontColor=GetRandomColor();

;

using(BitmapobjBitmap=this.GetVerifyCodeImage())

if(objBitmap!

=null)

using(MemoryStreamobjMS=newMemoryStream())

objBitmap.Save(objMS,ImageFormat.Jpeg);

HttpContext.Current.Response.ClearContent();

HttpContext.Current.Response.ContentType="

image/Jpeg"

HttpContext.Current.Response.BinaryWrite(objMS.ToArray());

HttpContext.Current.Response.Flush();

HttpContext.Current.Response.End();

result=true;

///获取问题

questionList"

默认数字加减验证<

publicKeyValuePair<

GetQuestion(Dictionary<

questionList=null)

if(questionList==null)

questionList=newDictionary<

();

varoperArray=newstring[]{"

+"

"

*"

num"

varleft=objRandom.Next(0,10);

varright=objRandom.Next(0,10);

varoper=operArray[objRandom.Next(0,operArray.Length)];

if(oper=="

stringkey=string.Format("

{0}+{1}=?

left,right);

stringval=(left+right).ToString();

questionList.Add(key,val);

elseif(oper=="

{0}×

{1}=?

stringval=(left*right).ToString();

else

varnum=objRandom.Next(1000,9999);

questionList.Add(num.ToString(),num.ToString());

returnquestionList.ToList()[objRandom.Next(0,questionList.Count)];

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 交通运输

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1