<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Drawing" %>
<script language="C#" runat="server">
	private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面

			string checkCode = CreateRandomCode(4);
			Session["VCode"] = checkCode.ToLower();
			CreateImage(checkCode);
		}
		private string CreateRandomCode(int CodeCount)
		{
			string allChar = "0,1,2,3,4,5,6,7,8,9"; 
			string[] allCharArray = allChar.Split(Convert.ToChar(",")); 
			string RandomCode = ""; 
			int temp = -1; 

			Random rand = new Random(); 
			for (int i=0;i<CodeCount;i++) 
			{ 
				if (temp != -1) 
				{ 
					rand = new Random(temp*i*((int) DateTime.Now.Ticks)); 
				} 

				int t = rand.Next(10); 

				while (temp == t) 
				{ 
					t = rand.Next(10); 
				} 

				if (t==10) 
				{ 
					Console.WriteLine(t.ToString() + "|" + i.ToString()); 
				} 
				temp = t; 
				RandomCode += allCharArray[t]; 
			} 

			return RandomCode; 

		}

		private void CreateImage(string checkCode)
		{
			int iwidth = (int)(checkCode.Length * 9.5);
			System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
			Graphics g = Graphics.FromImage(image);
			Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
			Brush b = new System.Drawing.SolidBrush(Color.Red);
			g.FillRectangle(new System.Drawing.SolidBrush(Color.LightBlue),0,0,image.Width, image.Height);
			//g.Clear(Color.DarkOrange);
			g.DrawString(checkCode, f, b, 3, 3);

			Pen blackPen = new Pen(Color.Black, 0);
			Random rand = new Random();
//			for (int i=0;i<5;i++)
//			{
//				int y = rand.Next(image.Height);
//				g.DrawLine(blackPen,0,y,image.Width,y);
//			}
			
            
			System.IO.MemoryStream ms = new System.IO.MemoryStream();
			image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
			Response.ClearContent();
			Response.ContentType = "images/test11.jpeg";
			Response.BinaryWrite(ms.ToArray());
			g.Dispose();
			image.Dispose();
		}
</script>