验证码,字体旋转。

Code.cs


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;

namespace NationalUnion.Member.Models
{
/// <summary>
/// 生成验证码的类
/// </summary>
public class ValidateCode
{

/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string CreateValidateCode(int length)
{
StringBuilder strCodeBuilder = new StringBuilder();
string strCode = "0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

List<char> vCharList = strCode.ToList<char>();
int maxValule = vCharList.Count() - 1;
for (int i = 0; i < length; i++)
{
Random rd = new Random();
int number=rd.Next(0, maxValule);
string strValue = vCharList[number].ToString();
strCodeBuilder.Append(strValue);
}
return strCodeBuilder.ToString();
}

public byte[] CreateValidateGraphicMatrix(string validateCode)
{
Bitmap image = new Bitmap(80, 30);
Graphics g = Graphics.FromImage(image);
try
{
Matrix matrix = new Matrix();//Matrix封装表示几何变换的 3 x 3 仿射矩阵

//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = 0; i < 25; 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.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);

char[] strValue = validateCode.ToCharArray();

for (int i = 0; i < strValue.Length; i++)
{
int y = 2;
if (i % 2 == 0)
{
y = -15 - i * 5;
matrix.Rotate(30);//对此 Matrix 沿原点顺时针旋转指定角度
}
else
{
matrix.Rotate(-30);//对此 Matrix 沿原点顺时针旋转指定角度
}
g.Transform = matrix;
g.DrawString(strValue[i].ToString(), font, brush, 20 + i * 15, y);
}

//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片流
return stream.ToArray();
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
}

Controller


using NationalUnion.Member.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NationalUnion.Member.Controllers
{
public class HomeController : Controller
{

public ActionResult Index()
{
return View();
}

public ActionResult ValidateCode()
{
ValidateCode vCode = new ValidateCode();
string code = vCode.CreateValidateCode(4);
Session["ValidateCode"] = code;
byte[] bytes = vCode.CreateValidateGraphicMatrix(code);
return File(bytes, @"image/jpeg");

}
}
}

Index.cshml


@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
<div>
<a href="@Url.Action("UserBasesicInfo","UserCenter")">进入用户中心</a>
<img id="valiCode" style="cursor: pointer;" src="@Url.Action("ValidateCode","Home")" alt="验证码" />
</div>

生成的效果:

  

时间: 2024-11-07 05:03:42

验证码,字体旋转。的相关文章

验证码的旋转与放缩代码

public void demo2() throws IOException { int w = 60; int h = 30; BufferedImage img = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB ); Graphics g = img.getGraphics(); //背景 g.setColor(Color.WHITE); g.fillRect(0, 0, w, h); //字体 g.setFont(new Font("a

css3 字体旋转

<style> #mycon { font-weight: bold; font-size: 150px; transform: rotateX(10deg); -webkit-transform: rotateX(10deg); /* Safari and Chrome */ -webkit-transform: rotateY(120deg); /* Safari and Chrome */ } #mycon1 { font-weight: bold; font-size: 150px;

PHP-解析验证码类--学习笔记

1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $charset = 'abcdefghjkmnprstuvwxyzABCDEFGJKMNPRSTUVWXYZ23456789'; private $code; private $codeLen = 4; private $width = 130; private $heigh = 50; private $

java创建图片验证码

package com; import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random; import javax.imageio.ImageIO;import javax.servlet.h

C#彩色扭曲验证码

该验证码生成类集合了网上大部分的验证码生成类的精华,博采众长并多次改进,现在已经形成了可在生产环节中使用的验证码. 该验证码加入了背景噪点,背景噪点曲线和直线,背景噪点文字以及扭曲,调暗,模糊等.完全可以实现防识别. 按照国际惯例先贴张效果图吧: #region 验证码生成类 /// <summary> /// 验证码生成类 /// </summary> public class ValidateCode { #region 定义和初始化配置字段 //用户存取验证码字符串 publ

验证码几个小例

例一: 在网站要目录下添加ValidateCode.aspx,路径自己定. ValidateCode.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidateCode.aspx.cs" Inherits="ValidateCode_ValidateCode" %> ValidateCode.aspx.cs: using System

Android本地验证码的生成

android客户端生成本地验证码主要用来限制用户随意按请求按钮,其实该示例也是来对自定义view的练练手而已,先给出效果图吧 其中可定制: *干扰线数目 *干扰点数目 *背景颜色 *验证码字体大小及字数 相信以上可以满足一般的需要了吧,不够的话可自行添加,下面就来讲实现的步骤了 继承view,重写构造方法,并初始化所需参数 public class ValidationCode extends View { private Paint mTextPaint;//文字画笔 private Pai

java实现简单的验证码(待增强)

package com.xxx; import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import javax.imageio.ImageIO;

网友写的验证码生成方案,可防止绝大多数机械识别。

web.xml Xml代码   <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <!-- ******************************************** --> <!-- ***