コード例 #1
0
        public static string  CreateAuthCodeImg(int width,int height,out string authcode)
        {
            string db="1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,g,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
            string[] dbStr=db.Split(',');
            string codeStr=string.Empty;
            Random rand=new Random();
            for(int i=0;i<4;i++)
            {
               codeStr+=dbStr[rand.Next(dbStr.Length)];
            }
            Bitmap map=new Bitmap(width,height);
            Graphics g=Graphics.FromImage(map);
            WebColorConverter color=new WebColorConverter();
            g.Clear((Color)color.ConvertFromString("#FAE264"));
            //图片背景噪音线
            for(int i=0;i<12;i++)
            {
                int x1=rand.Next(map.Width);
                int x2=rand.Next(map.Width);
                int y1=rand.Next(map.Height);
                int y2=rand.Next(map.Height);
                g.DrawLine(new Pen(Color.LightGray),x1,y1,x2,y2);
            }
            Font font=new Font("Arial",15,FontStyle.Regular|FontStyle.Italic);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            new Rectangle(0,0,map.Width,map.Height),Color.Blue,Color.Gray,1.2f,true);
            g.DrawString(codeStr,font,brush,0,0);
            authcode = codeStr;
            //图片前景噪音点
            //for(int i=0;i<10;i++)
            //{
            //    int x=rand.Next(map.Width);
            //    int y=rand.Next(map.Height);
            //    map.SetPixel(x,y,Color.White);
            //}
            //画图片边框
            //g.DrawRectangle(new Pen(Color.Black),0,0,map.Width-1,map.Height-1);
            
            //MemoryStream stream=new MemoryStream();
            //map.Save(stream,System.Drawing.Imaging.ImageFormat.Gif);
            //HttpContext.Current.Response.ClearContent();
            //HttpContext.Current.Response.ContentType = "image/gif";
            //HttpContext.Current.Response.BinaryWrite(stream.ToArray());
            
            
            string fileName="ViewPage\\Images\\AuthCodeImg\\"+DateTime.Now.ToFileTime()+".gif";
            string path = HttpContext.Current.Server.MapPath("~");
            map.Save(path+fileName);

            g.Dispose();
            map.Dispose();

            return "~//"+fileName.Replace("\\","/"); 
        }
コード例 #2
0
        /*产生验证图片*/
        public void CreateImages(string code)
        {
            Bitmap image = new Bitmap(60, 20);
            Graphics g = Graphics.FromImage(image);
            WebColorConverter ww = new WebColorConverter();
            g.Clear((Color)ww.ConvertFromString("#ffffff"));

            Random random = new Random();
            //画图片的背景噪音线
            for (int i = 0; i < 10; i++)
            {
                int x1 = random.Next(100);
                int x2 = random.Next(100);
                int y1 = random.Next(40);
                int y2 = random.Next(40);

                g.DrawLine(new Pen(Color.LightGray), x1, y1, x2, y2);
            }
            Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
            new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Gray, 1.2f, true);

            //画验证码字符串
            for (int i = 0; i < code.Length; i++)
            {
                g.DrawString(code[i].ToString(), font, brush, i * 14 + 2, 2);
            }

            //画图片的前景噪音点
            //for (int i = 0; i < 300; i++)
            //{
            //    int x = random.Next(image.Width);
            //    int y = random.Next(image.Height);
            //    image.SetPixel(x, y, Color.LightGray);
            //}

            //画图片的边框线
            g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);

            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AppendHeader("Pragma", "No-Cache");
            //将验证码图片写入内存流,并将其以 "image/Png" 格式输出
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            try
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                Response.ClearContent();
                Response.ContentType = "image/Png";
                Response.BinaryWrite(ms.ToArray());
            }
            finally
            {
                //显式释放资源
                g.Dispose();
                image.Dispose();

            }
        }
コード例 #3
0
ファイル: Channel.aspx.cs プロジェクト: hkiaipc/yh
 /// <summary>
 /// #ff00ff, #f0f  => color
 /// </summary>
 /// <param name="text"></param>
 /// <returns></returns>
 private Color ConvertStringToColor(string text)
 {
     if (!string.IsNullOrEmpty(text))
     {
         Color r = Color.White;
         WebColorConverter wcc = new WebColorConverter();
         text.Trim();
         text = text.Replace("#", "");
         if (text.Length == 6)
         {
             r = (Color)wcc.ConvertFromString("#" + text);
         }
         if (text.Length == 3)
         {
             text = string.Format("{0}{0}{1}{1}{2}{2}",
                 text[0], text[1], text[2]);
             r = (Color)wcc.ConvertFromString("#" + text);
         }
         return r;
     }
     return Color.White;
 }