用指针那是相当的快。。相当的爽。。。普通验证码基本1ms之内解决掉
这里暂时是RGB2L法,要RGB2H的改一下就好
编译好的类
Binary.rar
2.53KB
RAR
33次下载
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
namespace Binary
{
public class CSunsafe
{
private System.Drawing.Bitmap curBitmap;
private int[,] pic;
public void File_Init(string sFilename)
{
if (sFilename != null)
{
curBitmap = (Bitmap)Image.FromFile(sFilename);
}
}
public void BMP_Init(Bitmap BMP)
{
if (BMP != null)
{
curBitmap = BMP;
}
}
private double Max(double a, double b)
{
if (a > b)
{
return a;
}
return b;
}
private double Min(double a, double b)
{
if (a < b)
{
return a;
}
return b;
}
private double RGB2H(double r, double g, double b)
{
double h;
r /= 255.0;
g /= 255.0;
b /= 255.0;
double mi = this.Min(r, this.Min(g, b));
double ma = this.Max(r, this.Max(g, b));
h = 0;
double del_max = ma - mi;
if (del_max == 0.0)
{
return 0.0;
}
double del_r = ((ma - (r / 6.0)) + (del_max / 2.0)) / del_max;
double del_g = ((ma - (g / 6.0)) + (del_max / 2.0)) / del_max;
double del_b = ((ma - (b / 6.0)) + (del_max / 2.0)) / del_max;
if (r == ma)
{
h = del_b - del_g;
}
else if (g == ma)
{
h = (1.0/3.0 + del_r) - del_b;
}
else if (b == ma)
{
h = (2.0/3.0 + del_g) - del_r;
}
if (h < 0.0)
{
h++;
}
if (h > 1.0)
{
h--;
}
return h;
}
private double RGB2L(double r, double g, double b)
{
r /= 255.0;
g /= 255.0;
b /= 255.0;
double mi = this.Min(r, this.Min(g, b));
return ((this.Max(r, this.Max(g, b)) + mi) / 2.0);
}
public int[,] Binary(double value)
{
if (curBitmap != null)
{
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
pic = new int [bmpData.Width,bmpData.Height];
unsafe
{
byte* ptr = (byte*)(bmpData.Scan0);
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width; j++)
{
if (RGB2L(ptr[2], ptr[1], ptr[0]) < value)
{
pic[j, i] = 1;
}
else
{
pic[j, i] = 0;
}
ptr += 3;
}
ptr += bmpData.Stride - bmpData.Width * 3;
}
}
curBitmap.UnlockBits(bmpData);
return pic;
}
else
{
return null;
}
}
}
}
要编译本代码请允许不安全代码 如下图所示