夜的第七章

ASP.NET、C#、.NET开发资源、Javascript等等为主的开发备注收藏注意警告等的一个个人博客。

« Brushes 类备查。。。javascript

发一个MVC的验证码Action


public ActionResult Yanzhengma()
        {
            Bitmap bitmap = new Bitmap(150,30);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            Random rnd = new Random();
            Color[] color ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange,Color.Brown, Color.DarkBlue };


            for (int i = 0; i < 6; i++)
            {

                int x1 = rnd.Next(150);

                int y1 = rnd.Next(30);

                int x2 = rnd.Next(150);

                int y2 = rnd.Next(30);

                Color clr = color[rnd.Next(color.Length)];

                g.DrawLine(new Pen(clr), x1, y1, x2, y2);

            }


            for (int i = 0; i < 500; i++)
            {

                int x = rnd.Next(150);

                int y = rnd.Next(30);

                Color clr = color[rnd.Next(color.Length)];

                bitmap.SetPixel(x, y, clr);

            }


            FontFamily[] families = { new FontFamily("宋体"),new FontFamily("微软雅黑"),new FontFamily("Arial"),new FontFamily("黑体")};
            Brush[] brushs = {Brushes.ForestGreen,Brushes.DarkBlue,Brushes.DarkCyan,Brushes.MediumOrchid,Brushes.OrangeRed,Brushes.Sienna};
            
            //这里根据生成方法不同可生成具体字符串,为了方便写死了。
            char[] chars = "3CFGGH".ToCharArray();
/*
把上面这句换成下面这几句,就可以是中文验证了,呵呵
string str = "研究表明当车辆高速行驶的时候最大的阻力都来源于空气在时速达到时空气阻力占所有行车阻力的有一组数字值得每位车友关注实验表明当车速高于每小时的时候就有的油耗是用来克服空气阻力的空气阻力系数每降低油耗就会随之降低换句话说即便风阻系数只降低油耗也会有较大的改善这也就是为什么很多厂家都斥巨资建立自己的风洞实验室的原因";
            char[] chars = new char[4];
            for (int i = 0; i < 4; i++)
            {
                chars[i] = str[rnd.Next(str.Length - 1)];
            }
*/

            PointF pointF = new PointF(0, 0);
            for (int i = 0; i < chars.Length; i++)
            {
                g.DrawString(chars[i].ToString(), new Font(families[rnd.Next(families.Length - 1)], 14f,FontStyle.Italic), brushs[rnd.Next(brushs.Length - 1)], pointF);
                pointF.X += 30f;
            }

            g.Flush();
            g.Dispose();

            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Jpeg);

            return File(stream.ToArray(), "image/jpeg");

        }

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Arwen Build 81206



©2007 - 2010 www.leadnt.com 保留一些权利吧