Salut,
voici une source qui génère un Captcha
repris en partie de la source VB.NET de istamkenitra :
http://www.vbfrance.com/codes/GENERATEUR-IMAGES-CAPTCHA_42006.aspx
et remanié
Source / Exemple :
Image captcha(PictureBox pictureBoxCible, int nbCaractere, int Largeur, int Hauteur, out string captchaStr)
{
Random ran = new Random();
captchaStr = "";
string[] sArray = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P",
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
for (int i = 0; i < nbCaractere; i++)
{
int intRandom = ran.Next(0, sArray.Length);
captchaStr = captchaStr + sArray[intRandom];
if (captchaStr.Length > 8)
{
captchaStr = captchaStr.Remove(8, captchaStr.Length - 8);
}
}
Graphics g = pictureBoxCible.CreateGraphics();
//mesure de str
SizeF sizeff = g.MeasureString(captchaStr, new Font("Arial", 25, FontStyle.Strikeout, GraphicsUnit.Pixel), 300);
//rectangle aux dimensions de la chaine de caractere
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, Largeur, Hauteur); //new System.Drawing.Rectangle(0, 0, sizeff.Width, sizeff.Height);
Bitmap b = new Bitmap(Largeur, Hauteur, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
//graphics pour dessiner ds le bitmap
Graphics gr = Graphics.FromImage(b);
//dessin du background image
HatchBrush hatchBrush = new HatchBrush(HatchStyle.Wave, Color.PowderBlue, Color.Black);
gr.FillRectangle(hatchBrush, rect);
//dessin des caratceres
gr.DrawString(captchaStr, new Font("Arial", 30, FontStyle.Regular, GraphicsUnit.Point), Brushes.Yellow, 65, 0);
gr.DrawString(captchaStr, new Font("Arial", 30, FontStyle.Regular, GraphicsUnit.Point), Brushes.Tomato, 67, 1);
gr.DrawString("- - - - - ", new Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Point), Brushes.Violet, 50, 4);
gr.DrawString(" - - - - -", new Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Point), Brushes.Thistle, 100, -7);
return b;
}
Conclusion :
Je suis débutant en C#
donc vous savez ce qu'il ne faut pas dire :)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.