当前位置:编程学习 > C#/ASP.NET >>

C# 验证识别类

[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Runtime.InteropServices; 
 
namespace 验证码处理 

    class VerifyCode 
    { 
        public Bitmap bmpobj; 
 
        public VerifyCode(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); 
 
            ////输出 
            //GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned); 
            //bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject()); 
            //gch.Free(); 
        } 
 
        /// <summary> 
        /// 得到有效图形并调整为可平均分割的大小 
        /// </summary> 
   &
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,