using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace SK.MemberWeb
{
	/// <summary>
	/// VerifyCode 的摘要说明。
	/// </summary>
	public class VerifyCode : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			if (!IsPostBack)
			{
				//Random seedRnd = new Random();
				//double verifyCode= seedRnd.Next(10000, 99999);

				string verifyCode = Utils.CreateRndStr(5).ToUpper();

				Session["verifycode"]=verifyCode.ToString();
				int width = 60;
				if(Request.QueryString["width"]!=null)
					if(Request.QueryString["width"]!="")
					{
						width = Convert.ToInt32(Request.QueryString["width"]);
					}
				int height = 20;
				if(Request.QueryString["height"]!=null)
					if(Request.QueryString["height"]!="")
					{
						height = Convert.ToInt32(Request.QueryString["height"]);
					}
				float fontsize = 12;
				if(Request.QueryString["fontsize"]!=null)
					if(Request.QueryString["fontsize"]!="")
					{
						fontsize = Convert.ToSingle(Request.QueryString["fontsize"]);
					}
				string bgcolor = "black";
				if(Request.QueryString["bgcolor"]!=null)
					if(Request.QueryString["bgcolor"]!="")
					{
						bgcolor = Request.QueryString["bgcolor"];
					}
				string font = "宋体";
				if(Request.QueryString["font"]!=null)
					if(Request.QueryString["font"]!="")
					{
						font = Request.QueryString["font"];
					}
				/*if(!bgcolor.StartsWith("#"))
					bgcolor = "#"+bgcolor;*/
				string noisecolor = "grey";
				if(Request.QueryString["noisecolor"]!=null)
					if(Request.QueryString["noisecolor"]!="")
					{
						noisecolor = Request.QueryString["noisecolor"];
					}
				string fontcolor = "white";
				if(Request.QueryString["fontcolor"]!=null)
					if(Request.QueryString["fontcolor"]!="")
					{
						fontcolor = Request.QueryString["fontcolor"];
					}
				/*if(!noisecolor.StartsWith("#"))
					noisecolor = "#"+noisecolor;*/
				Bitmap image = new Bitmap ( width , height );

				Graphics g = Graphics.FromImage(image) ;

				//g.FillRectangle (new SolidBrush(Color.FromArgb(200,200,200)),0 ,0 ,100,40) ;
				g.FillRectangle (new SolidBrush(Color.FromName(bgcolor)),0 ,0 ,width,height) ;
				int x1;
				int x2;
				int y1;
				int y2;
				int h=1;
				Pen rndPen = new Pen (Color.FromName(noisecolor), 1 ) ;
				Random Rnd1 = new Random();
				while (h<100)
				{
					x1=Rnd1.Next(0,width);
					x2=Rnd1.Next(0,width);
					y1=Rnd1.Next(0,height+10);
					y2=Rnd1.Next(0,height+10);
					g . DrawLine ( rndPen , x1 , y1 , x1+2 , y1+2 ) ;
					h++;
				}
				//Font axesFont = new Font( "Comic Sans MS" , fontsize ) ;
				Font axesFont = new Font( font , fontsize ) ;

				Brush blackBrush = new SolidBrush (Color.FromName(fontcolor)) ;

				g.DrawString(verifyCode.ToString(),axesFont,blackBrush,5,-1 ) ;
				//循环写入乱七八糟文字。
				
				/*int x1;
				int x2;
				int y1;
				int y2;
				int h=1;
				Pen rndPen = new Pen ( Color.White , 1 ) ;
				Random Rnd1 = new Random();
				while (h<100)
				{
					x1=Rnd1.Next(0,60);
					x2=Rnd1.Next(0,60);
					y1=Rnd1.Next(0,30);
					y2=Rnd1.Next(0,30);
					g . DrawLine ( rndPen , x1 , y1 , x1+2 , y1+2 ) ;
					h++;
				}
				*/
			/*	Pen grayPen = new Pen ( Color.DimGray , 2 ) ;
				g.DrawLine ( grayPen , 0 , 0 , 0 , height ) ;
				g.DrawLine ( grayPen , width , 0 , width,height) ;
				g.DrawLine ( grayPen , 0 , 0 , width , 0 ) ;
				g.DrawLine ( grayPen , 0 , height*2 , width , height ) ;*/
				image.Save( Response.OutputStream,ImageFormat.Jpeg); 
			}//if

		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
