我试过在winform里面做过。web的我不会。顶你到上面去

解决方案 »

  1.   


    我就是要在winform里面呀,
      只不过是这个图片,用流把这个图片读下来,然后去识别呀
      

  2.   

    asp.net生成一个验证图片的静态类(含调用) 
    using System.IO; 
    using System.Drawing; 
    using System.Drawing.Drawing2D; 
    using System.Drawing.Imaging; 
    using System; 
    using System.Web.Caching; 
    /// <summary> 
    /// CreateCheckCodeImage 的摘要说明 
    /// 此类是一个静态类,不需要实例化 
    /// </summary> 
    public static class CreateCheckCodeImage 

        /// <summary> 
        /// 在内存中生成一个验证图片 
        /// </summary> 
        /// <returns>System.IO.MemoryStream</returns> 
        public static MemoryStream Production(string StrCode) 
        { 
            MemoryStream ms = new MemoryStream(); 
            if (string.Equals(StrCode, string.Empty)) 
            { 
                return ms; 
            } 
            Bitmap image = new Bitmap((int)Math.Ceiling(StrCode.Length * 16.5),22); 
            Graphics g = Graphics.FromImage(image); 
            try 
            { 
                System.Random random = new Random(); 
                g.Clear(Color.White); 
                //生成背景干扰点 
                for (int i = 0; i < 25; i++) 
                { 
                    int x1 = random.Next(image.Width); 
                    int x2 = random.Next(image.Width); 
                    int y1 = random.Next(image.Height); 
                    int y2 = random.Next(image.Height); 
                    g.DrawLine(new Pen(Color.Silver), x1, x2, y1, y2); 
                } 
                //生成验证字符 
                Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); 
                g.DrawString(StrCode, font, brush,2,2); 
                //生成前景干扰点 
                for (int i = 0; i < 100; i++) 
                { 
                    int x1 = random.Next(image.Width); 
                    int y1 = random.Next(image.Height); 
                    image.SetPixel(x1, y1, Color.FromArgb(random.Next())); 
                } 
                //化图片的边框 
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);             image.Save(ms, ImageFormat.Gif); 
                return ms; 
            } 
            finally 
            { 
                g.Dispose(); 
                image.Dispose(); 
            } 
        } 
        /// <summary> 
        /// 随即生成一段字符串,用作生成随即图片 
        /// </summary> 
        /// <returns>string</returns> 
        public static string GenerateCheckCode() 
        { 
            int number; 
            char code; 
            string CheckCode = string.Empty; 
            System.Random random = new System.Random(); 
            for (int i = 0; i < 5; i++) 
            { 
                number = random.Next(); 
                if (number % 2 == 0) 
                { 
                    code = (char)('0' + (char)(number % 10)); 
                } 
                else 
                { 
                    code = (char)('A' + (char)(number % 26)); 
                } 
                CheckCode += code.ToString(); 
            } 
            return CheckCode; 
        } 
    } //调用 
            string temStr = CreateCheckCodeImage.GenerateCheckCode(); 
            Response.ClearContent(); 
            Response.Cache.SetCacheability(HttpCacheability.NoCache); 
            //写入Cookie["CheckCodeStrV "]=temStr,以后对比的时候可以用此值 
            Response.Cookies.Add(new HttpCookie("CheckCodeStrV", temStr)); 
            Response.ContentType = "image/Gif"; 
            Response.BinaryWrite(CreateCheckCodeImage.Production(temStr).ToArray()); 
      

  3.   

    可以实现:
    添加个label 显示在这个上面 label的autosize改为false backcolor改为白色在paint事件添加如下代码:
      private void label1_Paint(object sender, PaintEventArgs e)
            {            Random rd = new Random();
                string VNum = Convert.ToString(rd.Next(1000, 9999));
                Graphics g = e.Graphics;
                g.Clear(Color.White);
               
                Font f = new Font("Tahoma", 9);
                SolidBrush s = new SolidBrush(Color.Red);
                g.DrawString(VNum, f, s, 3, 3);
             
            }也可以把它封装在一个方法中:
      public void F()
            {
                Random rd = new Random();
                string VNum = Convert.ToString(rd.Next(1000, 9999));
                Graphics g = label1.CreateGraphics();//创建label的画板
                g.Clear(Color.White);
                Font f = new Font("Tahoma", 9);
                SolidBrush s = new SolidBrush(Color.Red);
                g.DrawString(VNum, f, s, 3, 3);
            }
    这样把这方法放在按钮事件也能改变验证码的内容了
      

  4.   

    这个容易,读下来,先去色,二值化后,用PHOTOSHOP做特征库
    确定位置,比对就好,最相近的就是那个数字啦。
      

  5.   

    收藏过一个,但是彩色的实别率不高啊,不同地方还得自己慢慢调using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;namespace BallotAiying2
    {
        class UnCodebase
        {
            public Bitmap bmpobj;
            public UnCodebase(Bitmap pic)
            {
                bmpobj = new Bitmap(pic);    //转换为Format32bppRgb
            }        /**//// <summary>
            /// 根据RGB,计算灰度值
            /// </summary>
            /// <param name="posClr">Color值</param>
            /// <returns>灰度值,整型</returns>
            private int GetGrayNumColor(System.Drawing.Color posClr)
            {
                return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
            }        /**//// <summary>
            /// 灰度转换,逐点方式
            /// </summary>
            public void GrayByPixels()
            {
                for (int i = 0; i < bmpobj.Height; i++)
                {
                    for (int j = 0; j < bmpobj.Width; j++)
                    {
                        int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
                        bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
                    }
                }
            }        /**//// <summary>
            /// 去图形边框
            /// </summary>
            /// <param name="borderWidth"></param>
            public void ClearPicBorder(int borderWidth)
            {
                for (int i = 0; i < bmpobj.Height; i++)
                {
                    for (int j = 0; j < bmpobj.Width; j++)
                    {
                        if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
                            bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
                    }
                }
            }        /**//// <summary>
            /// 灰度转换,逐行方式
            /// </summary>
            public void GrayByLine()
            {
                Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
                BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);
                //    bmpData.PixelFormat = PixelFormat.Format24bppRgb;
                IntPtr scan0 = bmpData.Scan0;
                int len = bmpobj.Width * bmpobj.Height;
                int[] pixels = new int[len];
                Marshal.Copy(scan0, pixels, 0, len);            //对图片进行处理
                int GrayValue = 0;
                for (int i = 0; i < len; i++)
                {
                    GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
                    pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb();      //Color转byte
                }            bmpobj.UnlockBits(bmpData);
            }        /**//// <summary>
            /// 得到有效图形并调整为可平均分割的大小
            /// </summary>
            /// <param name="dgGrayValue">灰度背景分界值</param>
            /// <param name="CharsCount">有效字符数</param>
            /// <returns></returns>
            public void GetPicValidByValue(int dgGrayValue, int CharsCount)
            {
                int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
                int posx2 = 0; int posy2 = 0;
                for (int i = 0; i < bmpobj.Height; i++)      //找有效区
                {
                    for (int j = 0; j < bmpobj.Width; j++)
                    {
                        int pixelValue = bmpobj.GetPixel(j, i).R;
                        if (pixelValue < dgGrayValue)     //根据灰度值
                        {
                            if (posx1 > j) posx1 = j;
                            if (posy1 > i) posy1 = i;                        if (posx2 < j) posx2 = j;
                            if (posy2 < i) posy2 = i;
                        };
                    };
                };
                // 确保能整除
                int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount;   //可整除的差额数
                if (Span < CharsCount)
                {
                    int leftSpan = Span / 2;    //分配到左边的空列 ,如span为单数,则右边比左边大1
                    if (posx1 > leftSpan)
                        posx1 = posx1 - leftSpan;
                    if (posx2 + Span - leftSpan < bmpobj.Width)
                        posx2 = posx2 + Span - leftSpan;
                }
                //复制新图
                Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
                bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
            }
            
           
      

  6.   


     /**//// <summary>
            /// 得到有效图形,图形为类变量
            /// </summary>
            /// <param name="dgGrayValue">灰度背景分界值</param>
            /// <param name="CharsCount">有效字符数</param>
            /// <returns></returns>
            public void GetPicValidByValue(int dgGrayValue)
            {
                int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
                int posx2 = 0; int posy2 = 0;
                for (int i = 0; i < bmpobj.Height; i++)      //找有效区
                {
                    for (int j = 0; j < bmpobj.Width; j++)
                    {
                        int pixelValue = bmpobj.GetPixel(j, i).R;
                        if (pixelValue < dgGrayValue)     //根据灰度值
                        {
                            if (posx1 > j) posx1 = j;
                            if (posy1 > i) posy1 = i;                        if (posx2 < j) posx2 = j;
                            if (posy2 < i) posy2 = i;
                        };
                    };
                };
                //复制新图
                Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
                bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
            }        /**//// <summary>
            /// 得到有效图形,图形由外面传入
            /// </summary>
            /// <param name="dgGrayValue">灰度背景分界值</param>
            /// <param name="CharsCount">有效字符数</param>
            /// <returns></returns>
            public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
            {
                int posx1 = singlepic.Width; int posy1 = singlepic.Height;
                int posx2 = 0; int posy2 = 0;
                for (int i = 0; i < singlepic.Height; i++)      //找有效区
                {
                    for (int j = 0; j < singlepic.Width; j++)
                    {
                        int pixelValue = singlepic.GetPixel(j, i).R;
                        if (pixelValue < dgGrayValue)     //根据灰度值
                        {
                            if (posx1 > j) posx1 = j;
                            if (posy1 > i) posy1 = i;                        if (posx2 < j) posx2 = j;
                            if (posy2 < i) posy2 = i;
                        };
                    };
                };
                //复制新图
                Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
                return singlepic.Clone(cloneRect, singlepic.PixelFormat);
            }
            
            /**//// <summary>
            /// 平均分割图片
            /// </summary>
            /// <param name="RowNum">水平上分割数</param>
            /// <param name="ColNum">垂直上分割数</param>
            /// <returns>分割好的图片数组</returns>
            public Bitmap [] GetSplitPics(int RowNum,int ColNum)
            {
                if (RowNum == 0 || ColNum == 0)
                    return null;
                int singW = bmpobj.Width / RowNum;
                int singH = bmpobj.Height / ColNum;
                Bitmap [] PicArray=new Bitmap[RowNum*ColNum];            Rectangle cloneRect;
                for (int i = 0; i < ColNum; i++)      //找有效区
                {
                    for (int j = 0; j < RowNum; j++)
                    {
                        cloneRect = new Rectangle(j*singW, i*singH, singW , singH);
                        PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图
                    }
                }
                return PicArray;
            }        /**//// <summary>
            /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景
            /// </summary>
            /// <param name="singlepic">灰度图</param>
            /// <param name="dgGrayValue">背前景灰色界限</param>
            /// <returns></returns>
            public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
            {
                Color piexl;
                string code = "";
                for (int posy = 0; posy < singlepic.Height; posy++)
                    for (int posx = 0; posx < singlepic.Width; posx++)
                    {
                        piexl = singlepic.GetPixel(posx, posy);
                        if (piexl.R < dgGrayValue)    // Color.Black )
                            code = code + "1";
                        else
                            code = code + "0";
                    }
                return code;
            }
        }
    }
      

  7.   

    我想问个问题,读图片是也有个问题,因为验证码是动态生成的所以说它在物理上是不存在的,所以首先要将它转换成图片来进行解析,而你看看网上验证码属性后后缀显示的都是.asp/fsd.....其它东西,而不是标准的jpg或bmp,所以说这里也是个难点。
       楼主这个地方是怎么解决的啊,可以交流下么。我Email:[email protected]谢谢了!!