コード例 #1
0
        public void RenderImage(System.Web.HttpContext context)
        {
            RandomWord = SelectRandomWord(5);
            //context.Session["authcode"] = RandomWord;
            Cookie.SetCookie("authcode", RandomWord);
            Bitmap   objBMP      = new System.Drawing.Bitmap(80, 26);
            Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);

            objGraphics.Clear(BackGroundColor);


            // Instantiate object of brush with black color
            SolidBrush objBrush = new SolidBrush(RandomTextColor);

            Font   objFont = null;
            int    a;
            String myFont, str;

            //Creating an array for most readable yet cryptic fonts for OCR's
            // This is entirely up to developer's discretion
            String[] crypticFonts = new String[11];

            crypticFonts[0]  = "Arial";
            crypticFonts[1]  = "Verdana";
            crypticFonts[2]  = "微软雅黑";
            crypticFonts[3]  = "Impact";
            crypticFonts[4]  = "Haettenschweiler";
            crypticFonts[5]  = "Lucida Sans Unicode";
            crypticFonts[6]  = "Garamond";
            crypticFonts[7]  = "Courier New";
            crypticFonts[8]  = "Book Antiqua";
            crypticFonts[9]  = "Arial Narrow";
            crypticFonts[10] = "Estrangelo Edessa";

            //Loop to write the characters on image
            // with different fonts.
            for (a = 0; a <= RandomWord.Length - 1; a++)
            {
                myFont  = crypticFonts[new Random().Next(a)];
                objFont = new Font(myFont, 12, FontStyle.Bold | FontStyle.Italic);//| FontStyle.Strikeout);
                str     = RandomWord.Substring(a, 1);
                objGraphics.DrawString(str, objFont, objBrush, a * 13, 5);
                objGraphics.Flush();
            }
            var ms = new MemoryStream();

            objBMP.Save(ms, ImageFormat.Jpeg);
            context.Response.ClearContent();//.

            //清除该页输出缓存,设置该页无缓存
            context.Response.Buffer          = true;
            context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
            context.Response.Expires         = 0;
            context.Response.CacheControl    = "no-cache";
            context.Response.AppendHeader("Pragma", "No-Cache");

            context.Response.ContentType = "image/jpeg";
            //objBMP.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            context.Response.BinaryWrite(ms.ToArray());



            objFont.Dispose();
            objGraphics.Dispose();
            objBMP.Dispose();


            context.Response.End();
        }
コード例 #2
0
 public byte[] RenderImageMvc2(System.Web.HttpContext context, string checkCode)
 {
     //context.Session["authcode"] = checkCode;
     Cookie.SetCookie("authcode", checkCode);
     return(GetByteFormImages2(checkCode));
 }
コード例 #3
0
        //--------------------第二种方式
        public void RenderImage2(System.Web.HttpContext context)
        {
            string chkCode = string.Empty;

            //颜色列表,用于验证码、噪线、噪点
            //Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
            Color[] color = { Color.Gray, Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown };
            //字体列表,用于验证码
            string[] font = { "Arial", "Mongolian Baiti", "Times New Roman", "Tahoma", "Mangal", "Mongolian Baiti", "Courier", "PMingLiU", "Impact" };
            //验证码的字符集,去掉了一些容易混淆的字符
            char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
            Random rnd       = new Random();

            //生成验证码字符串
            for (int i = 0; i < 4; i++)
            {
                chkCode += character[rnd.Next(character.Length)];
            }
            //保存验证码
            //context.Session["authcode"] = chkCode;
            Cookie.SetCookie("authcode", chkCode);

            Bitmap   bmp = new Bitmap(100, 40);
            Graphics g   = Graphics.FromImage(bmp);

            g.Clear(Color.White);
            //画噪线
            for (int i = 0; i < 5; i++)
            {
                int   x1  = rnd.Next(100);
                int   y1  = rnd.Next(40);
                int   x2  = rnd.Next(100);
                int   y2  = rnd.Next(40);
                Color clr = color[rnd.Next(color.Length)];
                g.DrawLine(new Pen(clr), x1, y1, x2, y2);
            }
            //画验证码字符串
            for (int i = 0; i < chkCode.Length; i++)
            {
                string fnt = font[rnd.Next(font.Length)];
                Font   ft  = new Font(fnt, 18);
                Color  clr = color[rnd.Next(color.Length)];
                g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8);
            }
            //画噪点
            for (int i = 0; i < 150; i++)
            {
                int   x   = rnd.Next(bmp.Width);
                int   y   = rnd.Next(bmp.Height);
                Color clr = color[rnd.Next(color.Length)];
                bmp.SetPixel(x, y, clr);
            }
            //清除该页输出缓存,设置该页无缓存
            context.Response.Buffer          = true;
            context.Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
            context.Response.Expires         = 0;
            context.Response.CacheControl    = "no-cache";
            context.Response.AppendHeader("Pragma", "No-Cache");
            //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
            MemoryStream ms = new MemoryStream();

            try
            {
                bmp.Save(ms, ImageFormat.Png);
                context.Response.ClearContent();
                context.Response.ContentType = "image/Png";
                context.Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //显式释放资源
                bmp.Dispose();
                g.Dispose();
            }
        }