Control ASP.net

Hace unas semanas atrás di una conferencia sobre creación de controles de usuario y controles personalizados para ASP.net.

En esta, mostré un control personalizado que había realizado hace un tiempo atrás, el cual tiene como objetivo emular esta no tan nueva funcionalidad, pero siendo usada mas comúnmente últimamente en el logueo de usuarios. Básicamente, una imagen con caracteres que validan que el usuario no sea un robot, obligando al usuario (humano) deducir la imagen en cuestión y escribir estos caracteres en una caja de texto, finalizando la validación.

En definitiva, aquí van las tres clases que hacen esto posible (Desde el control que cree).

Clase: AutoImage.vb

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Text
Imports System.ComponentModel
Imports System.Web.UI.WebControls
Imports System.Drawing

Namespace SpecialTools.AutoImage

    <ToolboxBitmap("C:Documents and SettingsAdministratorMy DocumentsVisual Studio 2005ProjectsAutoImageTestAutoImageGenAutoImage.bmp")> _
    Public Class AutoImage
        Inherits WebControl
        Implements INamingContainer

#Region "Private Variables"

        Public Enum ImageFormat
            Gif = 0
            Jpg = 1
        End Enum

        Public Enum MaskLevel
            High = 1
            Medium = 2
            Low = 3
        End Enum

        Protected _PalletBuffer As New Web.UI.WebControls.Image
        Private _FontColor As String
        Private _Word As String
        Private _BackGroundColor As String
        Private _BorderColor As String
        Private _BorderWidth As Unit
        Private _FontX As Integer
        Private _FontY As Integer
        Private _RandomMask As Boolean
        Private _PicFormat As ImageFormat
        Private _MaskColor As String
        Private _MaskLevel As MaskLevel

#End Region

#Region "Propiedades"

#Region "Main Properties"

        ”’ <summary>
        ”’ Set or Get the text represented by the control
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Behavior"), Description("Set or Get the text represented by the control")> _
        Public Property Word() As String
            Get
                EnsureChildControls()
                Return _Word
            End Get
            Set(ByVal value As String)
                EnsureChildControls()
                _Word = value.Replace(";", ",").ToString()
                SetProperties()
            End Set
        End Property

#End Region

#Region "Overrides Properties"

        Public Overrides Property BorderColor() As System.Drawing.Color
            Get
                EnsureChildControls()
                Return Drawing.ColorTranslator.FromHtml(_BorderColor)
            End Get
            Set(ByVal value As System.Drawing.Color)
                EnsureChildControls()
                _BorderColor = Drawing.ColorTranslator.ToHtml(value)
                SetProperties()
            End Set
        End Property

        Public Overrides Property BorderWidth() As System.Web.UI.WebControls.Unit
            Get
                EnsureChildControls()
                Return _BorderWidth
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                EnsureChildControls()
                _BorderWidth = value
                SetProperties()
            End Set
        End Property

        Public Overrides Property ForeColor() As System.Drawing.Color
            Get
                EnsureChildControls()
                Return Drawing.ColorTranslator.FromHtml(_FontColor)
            End Get
            Set(ByVal value As Drawing.Color)
                EnsureChildControls()
                _FontColor = Drawing.ColorTranslator.ToHtml(value)
                SetProperties()
            End Set
        End Property

        Public Overrides Property Width() As System.Web.UI.WebControls.Unit
            Get
                EnsureChildControls()
                Return MyBase.Width
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                EnsureChildControls()
                _PalletBuffer.Width = value
                MyBase.Width = value
                SetProperties()
            End Set
        End Property

        Public Overrides Property Height() As System.Web.UI.WebControls.Unit
            Get
                EnsureChildControls()
                Return MyBase.Height
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                EnsureChildControls()
                _PalletBuffer.Height = value
                MyBase.Height = value
                SetProperties()
            End Set
        End Property

        Public Overrides Property BackColor() As System.Drawing.Color
            Get
                EnsureChildControls()
                Return Drawing.ColorTranslator.FromHtml(_BackGroundColor)
            End Get
            Set(ByVal value As System.Drawing.Color)
                EnsureChildControls()
                _BackGroundColor = Drawing.ColorTranslator.ToHtml(value)
                SetProperties()
            End Set
        End Property

        Public Overrides ReadOnly Property Font() As System.Web.UI.WebControls.FontInfo
            Get
                SetProperties()
                Return MyBase.Font
            End Get
        End Property

#End Region

#Region "Behavior Properties"

        ”’ <summary>
        ”’ Format of the output picture
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Behavior"), Description("Format of the output picture")> _
        Public Property PictureFormat() As ImageFormat
            Get
                EnsureChildControls()
                Return _PicFormat
            End Get
            Set(ByVal value As ImageFormat)
                EnsureChildControls()
                _PicFormat = value
                SetProperties()
            End Set
        End Property

        ”’ <summary>
        ”’ Represent the position over X axis for the text
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Behavior"), Description("Represent the position over X axis for the text")> _
        Public Property TextPositionX() As Integer
            Get
                EnsureChildControls()
                Return _FontX
            End Get
            Set(ByVal value As Integer)
                EnsureChildControls()
                _FontX = value
                SetProperties()
            End Set
        End Property

        ”’ <summary>
        ”’ Represent the position over Y axis for the text
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Behavior"), Description("Represent the position over Y axis for the text")> _
        Public Property TextPositionY() As Integer
            Get
                EnsureChildControls()
                Return _FontY
            End Get
            Set(ByVal value As Integer)
                EnsureChildControls()
                _FontY = value
                SetProperties()
            End Set
        End Property

#End Region

#Region "Mask Properties"

        ”’ <summary>
        ”’ Enable a randomized mask
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Mask Property"), Description("Enable a randomized mask")> _
        Public Property RandomMask() As Boolean
            Get
                EnsureChildControls()
                Return _RandomMask
            End Get
            Set(ByVal value As Boolean)
                EnsureChildControls()
                _RandomMask = value
                SetProperties()
            End Set
        End Property

        ”’ <summary>
        ”’ Default mask color
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Mask Property"), Description("Default mask color")> _
        Public Property MaskColor() As Drawing.Color
            Get
                EnsureChildControls()
                Return Drawing.ColorTranslator.FromHtml(_MaskColor)
            End Get
            Set(ByVal value As System.Drawing.Color)
                EnsureChildControls()
                _MaskColor = Drawing.ColorTranslator.ToHtml(value)
                SetProperties()
            End Set
        End Property

        ”’ <summary>
        ”’ Ammount of noise for apply to the mask
        ”’ </summary>
        ”’ <value></value>
        ”’ <returns></returns>
        ”’ <remarks></remarks>
        <Category("Mask Property"), Description("Ammount of noise for apply to the mask")> _
        Public Property MaskProperty() As MaskLevel
            Get
                EnsureChildControls()
                Return _MaskLevel
            End Get
            Set(ByVal value As MaskLevel)
                EnsureChildControls()
                _MaskLevel = value
                SetProperties()
            End Set
        End Property

#End Region

#End Region

#Region "Common Functions"

        Private Sub SetProperties()
            Dim _Helper As New NaturalHelp
            Dim _strResultBase64 As String

            With _PalletBuffer
                .ID = Me.ClientID & "_Pallet"
                .Visible = True

                _strResultBase64 = _Helper.EncryptToBase64(_PalletBuffer.Width.ToString() & _
                                        ";" & _PalletBuffer.Height.ToString() & _
                                        ";" & _Word & _
                                        ";" & _FontColor & _
                                        ";" & _BackGroundColor & _
                                        ";" & _BorderColor & _
                                        ";" & _BorderWidth.Value & _
                                        ";" & _FontX.ToString & _
                                        ";" & _FontY.ToString & _
                                        ";" & _RandomMask.ToString & _
                                        ";" & CType(_PicFormat, Integer) & _
                                        ";" & MyBase.Font.Name.ToString() & _
                                        ";" & MyBase.Font.Bold.ToString & _
                                        ";" & MyBase.Font.Italic.ToString & _
                                        ";" & MyBase.Font.Strikeout.ToString & _
                                        ";" & MyBase.Font.Underline.ToString & _
                                        ";" & MyBase.Font.Size.ToString() & _
                                        ";" & _MaskColor & _
                                        ";" & CType(_MaskLevel, Integer))
                .ImageUrl = "UltraToolsImageEngine.aspx?Image=" & _strResultBase64
            End With
        End Sub

#End Region

        Public Sub New()
            MyBase.Width = 100
            MyBase.Height = 40

            ‘_Word = "AutoImage from " & vbCrLf & "UltraTools"

            With _PalletBuffer
                .Width = 100
                .Height = 40
            End With

            _FontX = 1
            _FontY = 1

            _FontColor = Drawing.ColorTranslator.ToHtml(Drawing.Color.Black)
            _BackGroundColor = Drawing.ColorTranslator.ToHtml(Drawing.Color.White)
            _BorderColor = Drawing.ColorTranslator.ToHtml(Drawing.Color.White)
            _RandomMask = False
            _PicFormat = ImageFormat.Gif
            _MaskLevel = MaskLevel.Medium
            _MaskColor = Drawing.ColorTranslator.ToHtml(Drawing.Color.Blue)

            SetProperties()
        End Sub

        Protected Overrides Sub CreateChildControls()
            EnsureChildControls()
            Me.Controls.Add(_PalletBuffer)
        End Sub

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            SetProperties()
        End Sub

        Protected Overrides Function SaveViewState() As Object
            EnsureChildControls()

            Dim state(14) As Object
            Dim objBase As Object = MyBase.SaveViewState()

            state(0) = objBase
            state(1) = _PalletBuffer.Width
            state(2) = _PalletBuffer.Height
            state(3) = _Word
            state(4) = _FontColor
            state(5) = _BackGroundColor
            state(6) = _BorderColor
            state(7) = _BorderWidth
            state(8) = _FontX
            state(9) = _FontY
            state(10) = _RandomMask
            state(11) = _PicFormat
            state(12) = _MaskColor
            state(13) = _MaskLevel

            Return state
        End Function

        Protected Overrides Sub LoadViewState(ByVal savedState As Object)
            Dim state() As Object = CType(savedState, Object())
            MyBase.LoadViewState(state(0))
            EnsureChildControls()

            _PalletBuffer.Width = state(1)
            _PalletBuffer.Height = state(2)
            _Word = state(3)
            _FontColor = state(4)
            _BackGroundColor = state(5)
            _BorderColor = state(6)
            _BorderWidth = state(7)
            _FontX = state(8)
            _FontY = state(9)
            _RandomMask = state(10)
            _PicFormat = state(11)
            _MaskColor = state(12)
            _MaskLevel = state(13)
        End Sub

    End Class

End Namespace


2 comentarios on “Control ASP.net”

  1. Helson Vargas dice:

    hola seria posible me envies el codigo completo a mi correo grasias de ante mano

  2. Matias dice:

    En los 3 posts está el código completo.


Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s