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 System.Text; using System.Drawing;
public partial class CnCode : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //获取GB2312编码页(表) Encoding gb = Encoding.GetEncoding("gb2312");
private void CreateImage(string checkCode) { int iwidth = (int)(checkCode.Length * 25); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); Graphics g = Graphics.FromImage(image); Font f = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(Color.White); //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height); g.Clear(Color.Blue); g.DrawString(checkCode, f, b, 3, 3);
Pen blackPen = new Pen(Color.Black, 0); Random rand = new Random(); for (int i = 0; i < 4; 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 = "image/png"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); }