Monday, February 19, 2007

CatchGenerator



Aqui dejo el codigo fuente de un componente que sirve para generar textos distorsionados, aquellos que se ponen en las páginas de registro para evitar a los bots

 

//
// CatchGenerator 0.2
// by Horacio Nuñez Hernández
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;

namespace CatchApplicationTest
{
class CatchGenerator : Component
{

private Font font = SystemFonts.DefaultFont;

public Font Font
{
get { return font; }
set { font = value; }
}

//Creamos un generador de numeros aleatorios, utilizando como semilla elementos de la hora
//Es recomendable crearlo una sola vez (en la clase en lugar de hacerlo cada vez que se invoque el metodo principal)
private Random generator = new Random(DateTime.Now.Second * DateTime.Now.Millisecond);


public Bitmap getBitmap(Size size, string[] words)
{

Bitmap bm = new Bitmap(size.Width,size.Height);
Graphics gp = Graphics.FromImage(bm);

gp.SmoothingMode = SmoothingMode.HighQuality;

//brocha de fondo
HatchBrush hb = new HatchBrush(HatchStyle.DottedGrid, genColor(), genColor());

//dibujar fondo
gp.FillRectangle(hb, new Rectangle(0, 0, size.Width, size.Height));

//dibujamos las lineas de manera aleatoria
for (int i = 0; i < size.Height / 2; i++)
{
gp.DrawLine(genPen(), genLinePoint(size), genLinePoint(size));
gp.DrawLine(genPen(), genLinePoint(size), genLinePoint(size));
}

//seleccionamos una palabra aleatoriamente
int wordIndex = generator.Next(words.Length);
string word = words[wordIndex];

//tomamos una copia del objeto Font
Font mFont = font.Clone() as Font;

//calculamos el tamaño del texto
SizeF sSize = gp.MeasureString(word, mFont);

//Comprobamos que el texto puede ser dibujado sin salirse de los limites de la imagen
//Si no es posible, disminuimos el tamaño en una unidad, siendo 1 el tamaño minimo
//(con 0 se lanza una excepcion)
while (!((((int)sSize.Width) < size.Width) & (((int)sSize.Height) < size.Height)))
{
if (mFont.Size - 1 != 0)
{
mFont = new Font(mFont.FontFamily, mFont.Size - 1);

sSize = gp.MeasureString(word, mFont);
}
else
{
break;
}
}

//Obtenemos las coordenadas para situar el texto
PointF charPoint = new PointF(((size.Width - sSize.Width) / 2), (size.Height - sSize.Height) / 2);

for (int c = 0; c < word.Length; c++)
{
//preparamos las deformaciones
float angle = generator.Next(5);
gp.RotateTransform(angle);

SizeF chrSize = gp.MeasureString(word[c].ToString(), mFont);
gp.DrawString(word[c].ToString(), mFont, genBrush(), charPoint);

//cambiamos la posicion en el eje X
charPoint.X= charPoint.X + (chrSize.Width/2) + 1;

//corregimos las deformaciones
gp.ResetTransform();

}
//devolvemos la imagen
return bm;

}

private Point genLinePoint(Size size)
{
int randomX = this.generator.Next(size.Width);
int randomY = this.generator.Next(size.Height);

return new Point(randomX,randomY);
}

private Brush genBrush()
{
return genPen().Brush;
}

private Pen genPen()
{
return new Pen(genColor());
}

private Color genColor()
{
int red = this.generator.Next(255);
int green = this.generator.Next(255);
int blue = this.generator.Next(255);

return Color.FromArgb(red, green, blue);
}
}
}

1 comment:

Ondřej Sloup said...

Hi,

I like your blog. Nice spots. If you'll have time you can look on my page.