生成验证码图片

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SHBO.Common;

public partial class Manager_VerifyCode : BasePage
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!this.Page.IsPostBack)
        {
            
try
            {
                Image image 
= Image.FromFile(base.Server.MapPath("Manager/ManagerControl/Images/VerifyCodeBack.gif"));
                
int width = image.Width;
                
int height = image.Height;

                
string text = RandomCode.GetRandomCodeToLower(4);

                AccessHelper.WriteSession(
"ImageCode", text.Replace(" """));
                AccessHelper.WriteSession(
"VerifyCode", text.Replace(" """));

                Bitmap bitmap 
= new Bitmap(width, height, PixelFormat.Format24bppRgb);
                bitmap.SetResolution(85f, 72f);
                Graphics graphics 
= Graphics.FromImage(bitmap);
                graphics.Clear(Color.White);
                graphics.SmoothingMode 
= SmoothingMode.AntiAlias;
                graphics.DrawImage(image, 
new Rectangle(00, width, height), 00, width, height, GraphicsUnit.Pixel);
                
int[] numArray = new int[] { 0x10141210864 };
                Font font 
= null;
                
// SizeF ef = new SizeF();
                for (int i = 0; i < 0x10; i++)
                {
                    font 
= new Font("Arial", (float)numArray[i], FontStyle.Bold);
                    
if (((ushort)graphics.MeasureString(text, font).Width) < ((ushort)width))
                    {
                        
break;
                    }
                }
                
float num4 = height / 2;
                
float num5 = width / 2;
                StringFormat format 
= new StringFormat();
                format.Alignment 
= StringAlignment.Center;
                format.LineAlignment 
= StringAlignment.Center;
                graphics.DrawString(text, font, 
new SolidBrush(Color.Black), new PointF(num5 + 1f, num4 + 1f), format);
                
base.Response.ContentType = "image/jpeg";
                bitmap.Save(
base.Response.OutputStream, ImageFormat.Jpeg);
            }
            
catch (Exception exception)
            {
                
base.Response.Write(exception.Message);
            }
        }
    }
}

 

.aspx 无控件或代码

 

 

/// <summary>
        
/// 生成随机数,不区分大小写,返回大写状态的随机数字符串
        
/// </summary>
        
/// <param name="intValue">需要生产的随机数位数</param>
        
/// <returns>返回生成的随机数</returns>
        public static string GetRandomCodeToLower(int intValue)
        {
            
string strChar = "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,V,W,X,Y,Z";
            
string[] chrArray = strChar.Split(',');
            
string strNum = "";                    //
            int temp = -1;                       //记录上次随机数值,尽量避免产生几个一样的随机数
            
//采用一个简单的算法以保证生成随机数的不同
            Random rand = new Random();
            
for (int i = 1; i < intValue + 1; i++)
            {
                
if (temp != -1)
                {
                    rand 
= new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                
int t = rand.Next(35);
                
if (temp != -1 && temp == t)
                {
                    
return GetRandomCode(intValue); //递归
                }
                temp 
= t;
                strNum 
+= chrArray[t];
            }
            
return strNum.ToLower();//返回生成的随机数
        }

 

 

 

<img alt="" src=VerifyCode.aspx />

posted on 2008-10-17 23:10  原始部落  阅读(207)  评论(0编辑  收藏  举报