验证码 生成变形的文字


None.gifusing System;
None.gif
using System.IO;
None.gif
using System.Web;
None.gif
using System.Web.UI;
None.gif
using System.Drawing;
None.gif
using System.Drawing.Drawing2D;
None.gif
using System.Drawing.Imaging;
None.gif
namespace Mis.Pages
ExpandedBlockStart.gif
{
InBlock.gif    
public class think_test:System.Web.UI.Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif        
private Random rand = new Random();
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gif        
{    
InBlock.gif            
base.OnInit(e);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string checkCode = CreateRandomCode(8);
InBlock.gif            Session[
"CheckCode"= checkCode;
InBlock.gif            CreateImage(checkCode);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gif        
Web 窗体设计器生成的代码
InBlock.gif
InBlock.gif        
InBlock.gif        
private string CreateRandomCode(int codeCount)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z" ;
InBlock.gif            
string[] allCharArray = allChar.Split(',');
InBlock.gif            
string randomCode = "";
InBlock.gif            
int temp = -1;
InBlock.gif            
for(int i = 0; i < codeCount; i++)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if(temp != -1)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    rand 
= new Random(i*temp*((int)DateTime.Now.Ticks));
ExpandedSubBlockEnd.gif                }

InBlock.gif                
int t = rand.Next(35);
InBlock.gif                
if(temp == t)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    
return CreateRandomCode(codeCount);
ExpandedSubBlockEnd.gif                }

InBlock.gif                temp 
= t;
InBlock.gif                randomCode 
+= allCharArray[t];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return randomCode;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void TransformG(Graphics g)
ExpandedSubBlockStart.gif        
{
InBlock.gif            Matrix myMatrix 
= new Matrix();
InBlock.gif            
int num1=rand.Next(80,900);
InBlock.gif            
int num2=rand.Next(80,800);
InBlock.gif            
float dd1=(float)num1;
InBlock.gif            
float dd2=(float)num2;
InBlock.gif            dd1
=dd1/10000;
InBlock.gif            dd2
=dd2/10000;
InBlock.gif            
float f1 = (float)(dd1);
InBlock.gif            
float f2 = (float)(dd2);
InBlock.gif            myMatrix.Shear(f1,f2);
InBlock.gif            g.MultiplyTransform(myMatrix);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void CreateImage(string checkCode)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
int iwidth = (int)(checkCode.Length * 11.5);
InBlock.gif            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap(15046);
InBlock.gif            Graphics g 
= Graphics.FromImage(image);
InBlock.gif            g.SmoothingMode
=SmoothingMode.HighQuality;
InBlock.gif            Font f 
= new System.Drawing.Font("Arial"18, System.Drawing.FontStyle.Regular);
InBlock.gif            Brush b 
= new System.Drawing.SolidBrush(Color.LightSlateGray);
InBlock.gif            Rectangle rect 
= new Rectangle(0015046);
InBlock.gif            HatchBrush hatchBrush 
= new HatchBrush(
InBlock.gif                HatchStyle.SmallConfetti,
InBlock.gif                Color.LightGray,
InBlock.gif                Color.White);
InBlock.gif            g.FillRectangle(hatchBrush, rect);
ExpandedSubBlockStart.gif            Point[] myArray 
={
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46)),
InBlock.gif                                 
new Point(rand.Next(150),rand.Next(46))
ExpandedSubBlockEnd.gif                             }
;
InBlock.gif            Pen myPen 
= new Pen(Color.Blue,1);
InBlock.gif
InBlock.gif            GraphicsPath myPath 
= new GraphicsPath();
InBlock.gif            myPath.AddBeziers(myArray);
InBlock.gif            g.DrawPath(myPen, myPath);
InBlock.gif
InBlock.gif            SizeF size;
InBlock.gif            
float CPostion=1;
InBlock.gif            
for(int i=0;i<checkCode.Length;i++)
ExpandedSubBlockStart.gif            
{
InBlock.gif                TransformG(g);
InBlock.gif                size 
= g.MeasureString(checkCode[i].ToString(), f);
InBlock.gif                g.DrawString(checkCode[i].ToString(), f, b, CPostion, 
7);
InBlock.gif                CPostion
+=size.Width-1;
InBlock.gif                g.ResetTransform();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
InBlock.gif            image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
InBlock.gif            g.Dispose();
InBlock.gif            image.Dispose();
InBlock.gif            Response.ClearContent();
InBlock.gif            Response.ContentType 
= "image/Jpeg";
InBlock.gif            Response.BinaryWrite(ms.ToArray());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
    
ExpandedBlockEnd.gif}




本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/12/253059.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值