苹果收不到验证码怎样设置 C#创建简单的验证码操作
C#创建简单的验证码操作

首先 创建一个CLASS类 然后需要add Reference的方式添加 System Drawing(画画的类) 方法代码如下 /// <summary> /// 定义显示的随机字符 /// </summary> /// <param name= strList ></param> /// <returns></returns> private string imageStr(char[] strList) {
if (strList == null) strList = ABCDEFGHIJKLMNOPQRSTUVWXYZ ToCharArray();
int codeLengh = ; string radomCode = ; Random r = new Random(); for (int i = ; i < codeLengh;i++) { radomCode += strList[r Next(strList Length)]; } return radomCode; }
/// <summary> /// 创建随机验证字符的IMAGE 并保存 同时返回随机字符串 /// </summary> /// <param name= iWidth >图片宽度 时 默认为 </param> /// <param name= iHeight >图片高度 时 默认为 </param> /// <param name= font >字符字体 null时 默认为 Arial FontStyle Bold</param> /// <param name= sb >字符颜色 null时 默认为红</param> /// <param name= ImagePath >需要保存的文件绝对路径</param> /// <param name= strList >随即字符库 null时 默认为 A Z</param> /// <returns>返回随机字符串</returns> public string createImgWithStr(int iWidth int iHeight Font font SolidBrush sb string ImagePath char[] strList) { if (font == null) font = new Font( Arial FontStyle Bold); if (sb == null) sb = new SolidBrush(Color Red); if (iWidth == ) iWidth = ; if (iHeight == ) iHeight = ; //得到随机字符串 string imageString = imageStr(strList); //定义横向竖向都画 跳线 int lineCount = ; 这 支笔用来画线条的 Pen pen = new Pen(Color Gold ); Pen pen = new Pen(Color Black ); //定义图片 Bitmap image = new Bitmap(iWidth iHeight); //跟J ME一样的画笔 Graphics g = Graphics FromImage(image); //先画背景色 当然你可以自定义下 g Clear(ColorTranslator FromHtml( #F F F )); //确定写字的落点 Rectangle rect = new Rectangle( iWidth iHeight);
Random r = new Random();
//默认随机画横向竖向 条线 for(int i = ;i<lineCount;i++) { Point p = new Point( r Next(iHeight)); Point p = new Point(iWidth r Next(iHeight)); Point p = new Point(r Next(iWidth) ); Point p = new Point(r Next(iWidth) iHeight); g DrawLine(pen p p ); g DrawLine(pen p p ); } //写字 g DrawString(imageString font sb rect); //删除源文件 if (File Exists(ImagePath)) File Delete(ImagePath); //保存文件 我定义为jpeg格式 image Save(ImagePath System Drawing Imaging ImageFormat Jpeg); //释放资源 g Dispose(); image Dispose();
return imageString; } 另外 我在实际运用过程中总是发现重新生成了图片 但是显示却还是以前那张
lishixinzhi/Article/program/net/201311/11557