Cómo implementar código de verificación dinámica en VB NET
Importaciones?System.Data
Importaciones?System.Configuration
Importaciones?System.Collections
Importaciones?Sistema. Web
¿Importaciones?System.Web.Security
¿Importaciones?System.Web.UI
¿Importaciones?System.Web.UI.WebControls
¿Importaciones?System.Web.UI.WebControls.WebParts
¿Importaciones?System.Web.UI.HtmlControls
¿Importaciones?System.Drawing
¿Parcial? Public?Page_Load(ByVal?sender?As?Object, ?ByVal?e?As?System.EventArgs)
Session("VerifyCode")=?MakeValidateCode()
CreateImage( Session("VerifyCode").ToString())
Fin?Sub
Privado?Sub?CreateImage(ByVal?checkCode?As?String)
Dim ?iwidth?As?Integer?=?CInt(checkCode.Length*?12)? ?10
¿Dim?image?As?New?System.Drawing.Bitmap(iwidth,?25)
Dim?g?As?Graphics?=?Graphics.FromImage(imagen)
g.Clear( Color.White)
'Definir el color
¿Dim?c()?Como?Color?=?{?Color.Negro,?Color.Rojo,?Color.Azul Oscuro,?Color.Verde,?Color.Naranja,?Color.Marrón,?Color.Cian Oscuro ,?Color.Purple? }
'Definir fuente
Dim?font()?As?String?=?{?"Verdana",Microsoft?}
Dim?rand?As?New?Random()
'Aleatorizar el ruido de salida
For?i?As?Integer?=?0?To?4
Dim?x?As?Integer?=?rand.Next(imagen.Width)
Dim?y?As?Integer?=?rand.Next(imagen.Height)
g .DrawRectangle(New?Pen(Color.LightGray,?0),?x,?y,?1,?1)
Siguiente?i
'Generar diferentes fuentes y colores de caracteres del código de verificación
For?i?As?Integer?=?0?To?checkCode.Length?-?1
Dim?cindex?
As?Integer?=?rand.Next(7)
Dim?findex?As?Integer?=?rand.Next(5)
Dim?f?As?Font? =?New?System.Drawing.Font("Song",?12,?System.Drawing.FontStyle.Regular)
Dim?b?As?Brush=?New?System.Drawing.SolidBrush ( c(cindex))
Dim?ii?As?=?0 Entonces
ii?=?2
¿Fin?Si
g.DrawString(checkCode.Substring(i,?1),?f,?b,?3? (i?*?12),?ii)
Siguiente?i
'Dibujar un borde
g.DrawRectangle(New?Pen(Color.Black,?0),?0,?image.Width??1,?image.Height?1)
'Salida al navegador
Dim?ms?As?New?System.IO.MemoryStream()
image.Save(ms,?System.Drawing .Imaging. ImageFormat.Jpeg)
Respuesta.ClearContent()