日期:2014-05-18  浏览次数:20708 次

我刚从网上复制回来的,可是看半天都看不懂这个怎么调用,这个怎么调用?
[code=C#][/code]
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Text
{
  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>
&