Fork me on GitHub

李可

导航

验证码的生成

生成验证码

   public class CreateCode : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            using (Bitmap bm = new Bitmap(80, 25))//画板 bitmap
            {
                using (Graphics gp = Graphics.FromImage(bm)) //画笔 graphics
                {
                   // gp.FillRectangle(Brushes.Red, 0, 0, bm.Width, bm.Height);    //填充画板,颜色,从某点开始 宽度高度
              //      gp.FillRectangle(Brushes.White, 1, 1, bm.Width - 2, bm.Height - 2);

                    gp.DrawString(MakeCode(), new Font("楷体", 12), Brushes.Yellow,22,5);
                    bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将这个画板输出
                }
            }
        }

        public string MakeCode()
        {
            string result = "";
            string ku = "0123456789abcdefghijklmnopqistuvwxyzABCDEFGHIJKLMNOPQISTUVWXYZ";
            Random ra = new Random();
            for (int i = 0; i < 4; i++)
            {
                result += ku[ra.Next(0, ku.Length)];
            }
            return result;
        }
画板Bitmap 画笔Graphic 控制颜色 Brushs 画字,颜色,开始xy,宽高

 

posted on 2015-02-19 16:51  李可在江湖  阅读(309)  评论(0编辑  收藏  举报