public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor, int textcolor)
        {
            VerifyImageInfo verifyimage = new VerifyImageInfo();

            verifyimage.ImageFormat = ImageFormat.Jpeg;
            verifyimage.ContentType = "image/pjpeg";

            // 直接指定尺寸, 而不使用外部参数中的建议尺寸
            width  = 100;
            height = 32;

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            Graphics g = Graphics.FromImage(bitmap);

            g.SmoothingMode = SmoothingMode.HighSpeed;
            g.Clear(bgcolor);

            int fixedNumber = textcolor == 2 ? 60 : 0;
            Pen linePen     = new Pen(Color.FromArgb(Next(50) + fixedNumber, Next(50) + fixedNumber, Next(50) + fixedNumber), 1);

            SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100)));

            for (int i = 0; i < 3; i++)
            {
                g.DrawArc(linePen, Next(20) - 6, Next(20) - 6, Next(width) + 6, Next(height) + 6, Next(-100, 100), Next(-200, 200));
            }

            Graphics charg = Graphics.FromImage(charbmp);

            float charx = -18;

            for (int i = 0; i < code.Length; i++)
            {
                m.Reset();
                m.RotateAt(Next(50) - 25, new PointF(Next(3) + 7, Next(3) + 7));

                charg.Clear(Color.Transparent);
                charg.Transform = m;
                //定义前景色为黑色
                drawBrush.Color = Color.Black;

                charx = charx + 18 + Next(5);
                PointF drawPoint = new PointF(charx, 2.0F);
                charg.DrawString(code[i].ToString(), fonts[Next(fonts.Length - 1)], drawBrush, new PointF(0, 0));

                charg.ResetTransform();

                g.DrawImage(charbmp, drawPoint);
            }


            drawBrush.Dispose();
            g.Dispose();
            charg.Dispose();

            verifyimage.Image = bitmap;

            return(verifyimage);
        }
Пример #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            string vercode = CreateAuthStr(4, false);

            Session["vercode"] = vercode;
            VerifyImage     verifyimg     = new VerifyImage();
            VerifyImageInfo verifyimginfo = verifyimg.GenerateImage(vercode, 120, 30, System.Drawing.Color.White, 2);


            System.Drawing.Bitmap image = verifyimginfo.Image;

            System.Web.HttpContext.Current.Response.ContentType = verifyimginfo.ContentType;
            image.Save(this.Response.OutputStream, verifyimginfo.ImageFormat);
        }