欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#验证码识别类完整实例

程序员文章站 2023-11-20 22:07:22
本文实例讲述了c#验证码识别类。分享给大家供大家参考。具体实现方法如下: using system; using system.collections.gene...

本文实例讲述了c#验证码识别类。分享给大家供大家参考。具体实现方法如下:

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>
  /// <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);
  }
  /// <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;
  }
  /// <summary>
  /// 得到灰度图像前景背景的临界值 最大类间方差法
  /// </summary>
  /// <returns>前景背景的临界值</returns>
  public int getdggrayvalue()
  {
   int[] pixelnum = new int[256];   //图象直方图,共256个点
   int n, n1, n2;
   int total;        //total为总和,累计值
   double m1, m2, sum, csum, fmax, sb;  //sb为类间方差,fmax存储最大方差值
   int k, t, q;
   int threshvalue = 1;      // 阈值
   //生成直方图
   for (int i = 0; i < bmpobj.width; i++)
   {
    for (int j = 0; j < bmpobj.height; j++)
    {
     //返回各个点的颜色,以rgb表示
     pixelnum[bmpobj.getpixel(i, j).r]++;    //相应的直方图加1
    }
   }
   //直方图平滑化
   for (k = 0; k <= 255; k++)
   {
    total = 0;
    for (t = -2; t <= 2; t++)    //与附近2个灰度做平滑化,t值应取较小的值
    {
     q = k + t;
     if (q < 0)      //越界处理
      q = 0;
     if (q > 255)
      q = 255;
     total = total + pixelnum[q];  //total为总和,累计值
    }
    pixelnum[k] = (int)((float)total / 5.0 + 0.5);  //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值
   }
   //求阈值
   sum = csum = 0.0;
   n = 0;
   //计算总的图象的点数和质量矩,为后面的计算做准备
   for (k = 0; k <= 255; k++)
   {
    sum += (double)k * (double)pixelnum[k];  //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和
    n += pixelnum[k];      //n为图象总的点数,归一化后就是累积概率
   }
   fmax = -1.0;       //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行
   n1 = 0;
   for (k = 0; k < 256; k++)     //对每个灰度(从0到255)计算一次分割后的类间方差sb
   {
    n1 += pixelnum[k];     //n1为在当前阈值遍前景图象的点数
    if (n1 == 0) { continue; }    //没有分出前景后景
    n2 = n - n1;       //n2为背景图象的点数
    if (n2 == 0) { break; }    //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环
    csum += (double)k * pixelnum[k];  //前景的“灰度的值*其点数”的总和
    m1 = csum / n1;      //m1为前景的平均灰度
    m2 = (sum - csum) / n2;    //m2为背景的平均灰度
    sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2); //sb为类间方差
    if (sb > fmax)     //如果算出的类间方差大于前一次算出的类间方差
    {
     fmax = sb;      //fmax始终为最大类间方差(otsu)
     threshvalue = k;    //取最大类间方差时对应的灰度的k就是最佳阈值
    }
   }
   return threshvalue;
  }
  /// <summary>
  /// 去掉杂点(适合杂点/杂线粗为1)
  /// </summary>
  /// <param name="dggrayvalue">背前景灰色界限</param>
  /// <returns></returns>
  public void clearnoise(int dggrayvalue, int maxnearpoints)
  {
   color piexl;
   int neardots = 0;
   //逐点判断
   for (int i = 0; i < bmpobj.width; i++)
    for (int j = 0; j < bmpobj.height; j++)
    {
     piexl = bmpobj.getpixel(i, j);
     if (piexl.r < dggrayvalue)
     {
      neardots = 0;
      //判断周围8个点是否全为空
      if (i == 0 || i == bmpobj.width - 1 || j == 0 || j == bmpobj.height - 1) //边框全去掉
      {
       bmpobj.setpixel(i, j, color.fromargb(255, 255, 255));
      }
      else
      {
       if (bmpobj.getpixel(i - 1, j - 1).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i, j - 1).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i + 1, j - 1).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i - 1, j).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i + 1, j).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i - 1, j + 1).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i, j + 1).r < dggrayvalue) neardots++;
       if (bmpobj.getpixel(i + 1, j + 1).r < dggrayvalue) neardots++;
      }
      if (neardots < maxnearpoints)
       bmpobj.setpixel(i, j, color.fromargb(255, 255, 255)); //去掉单点 && 粗细小3邻边点
     }
     else //背景
      bmpobj.setpixel(i, j, color.fromargb(255, 255, 255));
    }
  }
  /// <summary>
  /// 3×3中值滤波除杂
  /// </summary>
  /// <param name="dggrayvalue"></param>
  public void clearnoise(int dggrayvalue)
  {
   int x, y;
   byte[] p = new byte[9]; //最小处理窗口3*3
   byte s;
   //byte[] lptemp=new byte[nbytewidth*nheight];
   int i, j;
   //--!!!!!!!!!!!!!!下面开始窗口为3×3中值滤波!!!!!!!!!!!!!!!!
   for (y = 1; y < bmpobj.height - 1; y++) //--第一行和最后一行无法取窗口
   {
    for (x = 1; x < bmpobj.width - 1; x++)
    {
     //取9个点的值
     p[0] = bmpobj.getpixel(x - 1, y - 1).r;
     p[1] = bmpobj.getpixel(x, y - 1).r;
     p[2] = bmpobj.getpixel(x + 1, y - 1).r;
     p[3] = bmpobj.getpixel(x - 1, y).r;
     p[4] = bmpobj.getpixel(x, y).r;
     p[5] = bmpobj.getpixel(x + 1, y).r;
     p[6] = bmpobj.getpixel(x - 1, y + 1).r;
     p[7] = bmpobj.getpixel(x, y + 1).r;
     p[8] = bmpobj.getpixel(x + 1, y + 1).r;
     //计算中值
     for (j = 0; j < 5; j++)
     {
      for (i = j + 1; i < 9; i++)
      {
       if (p[j] > p[i])
       {
        s = p[j];
        p[j] = p[i];
        p[i] = s;
       }
      }
     }
     //  if (bmpobj.getpixel(x, y).r < dggrayvalue)
     bmpobj.setpixel(x, y, color.fromargb(p[4], p[4], p[4]));  //给有效值付中值
    }
   }
  }
  /// <summary>
  /// 该函数用于对图像进行腐蚀运算。结构元素为水平方向或垂直方向的三个点,
  /// 中间点位于原点;或者由用户自己定义3×3的结构元素。
  /// </summary>
  /// <param name="dggrayvalue">前后景临界值</param>
  /// <param name="nmode">腐蚀方式:0表示水平方向,1垂直方向,2自定义结构元素。</param>
  /// <param name="structure"> 自定义的3×3结构元素</param>
  public void erosionpic(int dggrayvalue, int nmode, bool[,] structure)
  {
   int lwidth = bmpobj.width;
   int lheight = bmpobj.height;
   bitmap newbmp = new bitmap(lwidth, lheight);
   int i, j, n, m;    //循环变量
   if (nmode == 0)
   {
    //使用水平方向的结构元素进行腐蚀
    // 由于使用1×3的结构元素,为防止越界,所以不处理最左边和最右边
    // 的两列像素
    for (j = 0; j < lheight; j++)
    {
     for (i = 1; i < lwidth - 1; i++)
     {
      //目标图像中的当前点先赋成黑色
      newbmp.setpixel(i, j, color.black);
 
      //如果源图像中当前点自身或者左右有一个点不是黑色,
      //则将目标图像中的当前点赋成白色
      if (bmpobj.getpixel(i - 1, j).r > dggrayvalue ||
       bmpobj.getpixel(i, j).r > dggrayvalue ||
       bmpobj.getpixel(i + 1, j).r > dggrayvalue)
       newbmp.setpixel(i, j, color.white);
     }
    }
   }
   else if (nmode == 1)
   {
    //使用垂真方向的结构元素进行腐蚀
    // 由于使用3×1的结构元素,为防止越界,所以不处理最上边和最下边
    // 的两行像素
    for (j = 1; j < lheight - 1; j++)
    {
     for (i = 0; i < lwidth; i++)
     {
      //目标图像中的当前点先赋成黑色
      newbmp.setpixel(i, j, color.black);
      //如果源图像中当前点自身或者左右有一个点不是黑色,
      //则将目标图像中的当前点赋成白色
      if (bmpobj.getpixel(i, j - 1).r > dggrayvalue ||
       bmpobj.getpixel(i, j).r > dggrayvalue ||
       bmpobj.getpixel(i, j + 1).r > dggrayvalue)
       newbmp.setpixel(i, j, color.white);
     }
    }
   }
   else
   {
    if (structure.length != 9) //检查自定义结构
     return;
    //使用自定义的结构元素进行腐蚀
    // 由于使用3×3的结构元素,为防止越界,所以不处理最左边和最右边
    // 的两列像素和最上边和最下边的两列像素
    for (j = 1; j < lheight - 1; j++)
    {
     for (i = 1; i < lwidth - 1; i++)
     {
      //目标图像中的当前点先赋成黑色
      newbmp.setpixel(i, j, color.black);
      //如果原图像中对应结构元素中为黑色的那些点中有一个不是黑色,
      //则将目标图像中的当前点赋成白色
      for (m = 0; m < 3; m++)
      {
       for (n = 0; n < 3; n++)
       {
        if (!structure[m, n])
         continue;
        if (bmpobj.getpixel(i + m - 1, j + n - 1).r > dggrayvalue)
        {
         newbmp.setpixel(i, j, color.white);
         break;
        }
       }
      }
     }
    }
   }
   bmpobj = newbmp;
  }
  /// <summary>
  /// 该函数用于对图像进行细化运算。要求目标图像为灰度图像
  /// </summary>
  /// <param name="dggrayvalue"></param>
  public void thiningpic(int dggrayvalue)
  {
   int lwidth = bmpobj.width;
   int lheight = bmpobj.height;
   // bitmap newbmp = new bitmap(lwidth, lheight);
   bool bmodified;    //脏标记  
   int i, j, n, m;    //循环变量
   //四个条件
   bool bcondition1;
   bool bcondition2;
   bool bcondition3;
   bool bcondition4;
   int ncount;  //计数器  
   int[,] neighbour = new int[5, 5];  //5×5相邻区域像素值
   bmodified = true;
   while (bmodified)
   {
    bmodified = false;
    //由于使用5×5的结构元素,为防止越界,所以不处理外围的几行和几列像素
    for (j = 2; j < lheight - 2; j++)
    {
     for (i = 2; i < lwidth - 2; i++)
     {
      bcondition1 = false;
      bcondition2 = false;
      bcondition3 = false;
      bcondition4 = false;
      if (bmpobj.getpixel(i, j).r > dggrayvalue)
      {
       if (bmpobj.getpixel(i, j).r < 255)
        bmpobj.setpixel(i, j, color.white);
       continue;
      }
      //获得当前点相邻的5×5区域内像素值,白色用0代表,黑色用1代表
      for (m = 0; m < 5; m++)
      {
       for (n = 0; n < 5; n++)
       {
        neighbour[m, n] = bmpobj.getpixel(i + m - 2, j + n - 2).r < dggrayvalue ? 1 : 0;
       }
      }
      //逐个判断条件。
      //判断2<=nz(p1)<=6
      ncount = neighbour[1, 1] + neighbour[1, 2] + neighbour[1, 3]
        + neighbour[2, 1] + neighbour[2, 3] +
        +neighbour[3, 1] + neighbour[3, 2] + neighbour[3, 3];
      if (ncount >= 2 && ncount <= 6)
      {
       bcondition1 = true;
      }
      //判断z0(p1)=1
      ncount = 0;
      if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
       ncount++;
      if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
       ncount++;
      if (neighbour[2, 1] == 0 && neighbour[3, 1] == 1)
       ncount++;
      if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
       ncount++;
      if (neighbour[3, 2] == 0 && neighbour[3, 3] == 1)
       ncount++;
      if (neighbour[3, 3] == 0 && neighbour[2, 3] == 1)
       ncount++;
      if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
       ncount++;
      if (neighbour[1, 3] == 0 && neighbour[1, 2] == 1)
       ncount++;
      if (ncount == 1)
       bcondition2 = true;
      //判断p2*p4*p8=0 or z0(p2)!=1
      if (neighbour[1, 2] * neighbour[2, 1] * neighbour[2, 3] == 0)
      {
       bcondition3 = true;
      }
      else
      {
       ncount = 0;
       if (neighbour[0, 2] == 0 && neighbour[0, 1] == 1)
        ncount++;
       if (neighbour[0, 1] == 0 && neighbour[1, 1] == 1)
        ncount++;
       if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
        ncount++;
       if (neighbour[2, 1] == 0 && neighbour[2, 2] == 1)
        ncount++;
       if (neighbour[2, 2] == 0 && neighbour[2, 3] == 1)
        ncount++;
       if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
        ncount++;
       if (neighbour[1, 3] == 0 && neighbour[0, 3] == 1)
        ncount++;
       if (neighbour[0, 3] == 0 && neighbour[0, 2] == 1)
        ncount++;
       if (ncount != 1)
        bcondition3 = true;
      }
      //判断p2*p4*p6=0 or z0(p4)!=1
      if (neighbour[1, 2] * neighbour[2, 1] * neighbour[3, 2] == 0)
      {
       bcondition4 = true;
      }
      else
      {
       ncount = 0;
       if (neighbour[1, 1] == 0 && neighbour[1, 0] == 1)
        ncount++;
       if (neighbour[1, 0] == 0 && neighbour[2, 0] == 1)
        ncount++;
       if (neighbour[2, 0] == 0 && neighbour[3, 0] == 1)
        ncount++;
       if (neighbour[3, 0] == 0 && neighbour[3, 1] == 1)
        ncount++;
       if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
        ncount++;
       if (neighbour[3, 2] == 0 && neighbour[2, 2] == 1)
        ncount++;
       if (neighbour[2, 2] == 0 && neighbour[1, 2] == 1)
        ncount++;
       if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
        ncount++;
       if (ncount != 1)
        bcondition4 = true;
      }
      if (bcondition1 && bcondition2 && bcondition3 && bcondition4)
      {
       bmpobj.setpixel(i, j, color.white);
       bmodified = true;
      }
      else
      {
       bmpobj.setpixel(i, j, color.black);
      }
     }
    }
   }
   // 复制细化后的图像
   //  bmpobj = newbmp;
  }
  /// <summary>
  /// 锐化要启用不安全代码编译
  /// </summary>
  /// <param name="val">锐化程度。取值[0,1]。值越大锐化程度越高</param>
  /// <returns>锐化后的图像</returns>
  public void sharpen(float val)
  {
   int w = bmpobj.width;
   int h = bmpobj.height;
   bitmap bmprtn = new bitmap(w, h, pixelformat.format24bpprgb);
   bitmapdata srcdata = bmpobj.lockbits(new rectangle(0, 0, w, h), imagelockmode.readonly, pixelformat.format24bpprgb);
   bitmapdata dstdata = bmprtn.lockbits(new rectangle(0, 0, w, h), imagelockmode.writeonly, pixelformat.format24bpprgb);
   unsafe
   {
    byte* pin = (byte*)srcdata.scan0.topointer();
    byte* pout = (byte*)dstdata.scan0.topointer();
    int stride = srcdata.stride;
    byte* p;
    for (int y = 0; y < h; y++)
    {
     for (int x = 0; x < w; x++)
     {
      //取周围9点的值。位于边缘上的点不做改变。
      if (x == 0 || x == w - 1 || y == 0 || y == h - 1)
      {
       //不做
       pout[0] = pin[0];
       pout[1] = pin[1];
       pout[2] = pin[2];
      }
      else
      {
       int r1, r2, r3, r4, r5, r6, r7, r8, r0;
       int g1, g2, g3, g4, g5, g6, g7, g8, g0;
       int b1, b2, b3, b4, b5, b6, b7, b8, b0;
       float vr, vg, vb;
       //左上
       p = pin - stride - 3;
       r1 = p[2];
       g1 = p[1];
       b1 = p[0];
       //正上
       p = pin - stride;
       r2 = p[2];
       g2 = p[1];
       b2 = p[0];
       //右上
       p = pin - stride + 3;
       r3 = p[2];
       g3 = p[1];
       b3 = p[0];
       //左侧
       p = pin - 3;
       r4 = p[2];
       g4 = p[1];
       b4 = p[0];
       //右侧
       p = pin + 3;
       r5 = p[2];
       g5 = p[1];
       b5 = p[0];
       //右下
       p = pin + stride - 3;
       r6 = p[2];
       g6 = p[1];
       b6 = p[0];
       //正下
       p = pin + stride;
       r7 = p[2];
       g7 = p[1];
       b7 = p[0];
       //右下
       p = pin + stride + 3;
       r8 = p[2];
       g8 = p[1];
       b8 = p[0];
       //自己
       p = pin;
       r0 = p[2];
       g0 = p[1];
       b0 = p[0];
       vr = (float)r0 - (float)(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8) / 8;
       vg = (float)g0 - (float)(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8) / 8;
       vb = (float)b0 - (float)(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 8;
       vr = r0 + vr * val;
       vg = g0 + vg * val;
       vb = b0 + vb * val;
       if (vr > 0)
       {
        vr = math.min(255, vr);
       }
       else
       {
        vr = math.max(0, vr);
       }
       if (vg > 0)
       {
        vg = math.min(255, vg);
       }
       else
       {
        vg = math.max(0, vg);
       }
       if (vb > 0)
       {
        vb = math.min(255, vb);
       }
       else
       {
        vb = math.max(0, vb);
       }
       pout[0] = (byte)vb;
       pout[1] = (byte)vg;
       pout[2] = (byte)vr;
      }
      pin += 3;
      pout += 3;
     }// end of x
     pin += srcdata.stride - w * 3;
     pout += srcdata.stride - w * 3;
    } // end of y
   }
   bmpobj.unlockbits(srcdata);
   bmprtn.unlockbits(dstdata);
   bmpobj = bmprtn;
  }
  /// <summary>
  /// 图片二值化
  /// </summary>
  /// <param name="hsb"></param>
  public void bitmapto1bpp(double hsb)
  {
   int w = bmpobj.width;
   int h = bmpobj.height;
   bitmap bmp = new bitmap(w, h, pixelformat.format1bppindexed);
   bitmapdata data = bmp.lockbits(new rectangle(0, 0, w, h), imagelockmode.readwrite, pixelformat.format1bppindexed);
   for (int y = 0; y < h; y++)
   {
    byte[] scan = new byte[(w + 7) / 8];
    for (int x = 0; x < w; x++)
    {
     color c = bmpobj.getpixel(x, y);
     if (c.getbrightness() >= hsb) scan[x / 8] |= (byte)(0x80 >> (x % 8));
    }
    marshal.copy(scan, 0, (intptr)((int)data.scan0 + data.stride * y), scan.length);
   }
   bmp.unlockbits(data);
   bmpobj = bmp;
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。