Control ASP.net parte II

Clase: ImageEngine.vb

Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class ImageEngine
    : Implements IHttpHandler

    Private Const _TotalParams As Integer = 19

    Private _ImageWidth As Integer
    Private _ImageHeight As Integer
    Private _Word As String
    Private _FontColor As Brush
    Private _BackGroundColor As Color
    Private _BorderColor As Pen
    Private _FontPostX As Integer
    Private _FontPostY As Integer
    Private _BorderWidth As Single
    Private _RandomMask As Boolean
    Private _PicFormat As Boolean
    Private _MaskColor As Pen
    Private _MaskLevel As Integer

    Private _FontType As String
    Private _FontStyle As Integer = 0
    Private _FontSize As Integer

    Private Function AssignValues(ByVal strValueString As String) As Boolean
        Dim strSplitter() As String = strValueString.Split(";")
        _FontStyle = 0

        If strSplitter.Length = _TotalParams Then
            Try
                _ImageWidth = Unit.Parse(strSplitter(0)).Value
                _ImageHeight = Unit.Parse(strSplitter(1)).Value
                _Word = CType(strSplitter(2), String)
                _FontColor = New System.Drawing.SolidBrush(ColorTranslator.FromHtml(CType(strSplitter(3), String)))
                _BackGroundColor = ColorTranslator.FromHtml(CType(strSplitter(4), String))
                _BorderWidth = CType(strSplitter(6), Single)
                _BorderColor = New System.Drawing.Pen(ColorTranslator.FromHtml(CType(strSplitter(5), String)), CType(strSplitter(6), Single))
                _FontPostX = CType(strSplitter(7), Integer)
                _FontPostY = CType(strSplitter(8), Integer)
                _RandomMask = CType(strSplitter(9), Boolean)
                _PicFormat = CType(strSplitter(10), Boolean)
                _FontType = CType(strSplitter(11), String)
                If _FontType.Length = 0 Then _FontType = "Time New Roman"
                If CType(strSplitter(12), Boolean) Then _FontStyle += 1
                If CType(strSplitter(13), Boolean) Then _FontStyle += 2
                If CType(strSplitter(14), Boolean) Then _FontStyle += 8
                If CType(strSplitter(15), Boolean) Then _FontStyle += 4

                Select Case strSplitter(16)
                    Case "Larger", "X-Large", "XX-Large"
                        _FontSize = 14
                    Case "Smaller"
                        _FontSize = 8
                    Case "XX-Small", "X-Small", "Small"
                        _FontSize = 6
                    Case "Medium"
                        _FontSize = 10
                    Case "Large"
                        _FontSize = 12
                    Case Else
                        _FontSize = Unit.Parse(strSplitter(16)).Value
                End Select
                If _FontSize = 0 Then _FontSize = 8

                _MaskColor = New System.Drawing.Pen(ColorTranslator.FromHtml(CType(strSplitter(17), String)))
                _MaskLevel = CType(strSplitter(18), Integer) * 5

                Return True
            Catch ex As Exception
                Throw ex
            End Try
        Else
            Throw New Exception("Bad parameters")
        End If

    End Function

    Private Function ContextIsValid(ByVal strContext As String) As Boolean
        If strContext <> Nothing AndAlso strContext.Length > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub ProcessRequest(ByVal Context As HttpContext) Implements IHttpHandler.ProcessRequest
        Try
            If ContextIsValid(Context.Request.QueryString("Image")) Then
                Dim _Helper As NaturalHelp = New NaturalHelp
                If AssignValues(_Helper.DecryptToBase64(Context.Request.QueryString("Image"))) Then

                    If _PicFormat Then
                        Context.Response.ContentType = "image/jpeg"
                    Else
                        Context.Response.ContentType = "image/gif"
                    End If

                    ‘Create buffer and set size
                    Dim oBitmap As Bitmap = New Bitmap(_ImageWidth, _ImageHeight)
                    Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)

                    ‘Set Background Color
                    oGraphic.Clear(_BackGroundColor)

                    ‘Assing Word, Font Color, Font Size, Font Family
                    ‘Setting the font style
                    oGraphic.DrawString(_Word, _
                            New System.Drawing.Font(_FontType, _FontSize, _FontStyle, GraphicsUnit.Point), _
                            _FontColor, _FontPostX, _FontPostY)

                    If _RandomMask Then
                        Dim _RND As New Random(10)
                        Dim _RndNumber As Integer
                        Dim _blnOrientation As Boolean = False

                        For i As Integer = 0 To _ImageWidth
                            For j As Integer = 0 To _ImageHeight
                                _RndNumber = _RND.Next(0, _MaskLevel)
                                If _RndNumber = 3 Then
                                    If _blnOrientation Then
                                        oGraphic.DrawLine(_MaskColor, i, j, i + 1, j – 1)
                                    Else
                                        oGraphic.DrawLine(_MaskColor, i, j, i – 1, j + 1)
                                    End If
                                    _blnOrientation = Not _blnOrientation
                                End If
                            Next
                        Next
                    End If

                    ‘Create Border
                    If _BorderWidth <> 0 Then
                        oGraphic.DrawLine(_BorderColor, 0, 0, _ImageWidth, 0)
                        oGraphic.DrawLine(_BorderColor, 0, _ImageHeight – 1, _ImageWidth, _ImageHeight – 1)
                        oGraphic.DrawLine(_BorderColor, 0, 0, 0, _ImageHeight – 1)
                        oGraphic.DrawLine(_BorderColor, _ImageWidth – 1, 0, _ImageWidth – 1, _ImageHeight – 1)
                    End If

                    ‘Write all into the buffer
                    If _PicFormat Then
                        oBitmap.Save(Context.Response.OutputStream, ImageFormat.Jpeg)
                    Else
                        oBitmap.Save(Context.Response.OutputStream, ImageFormat.Gif)
                    End If

                End If
            End If
        Catch ex As Exception
            Context.Response.ContentType = "image/jpeg"
            Dim oBitmap As Bitmap = New Bitmap(150, 50)
            Dim oGraphic As Graphics = Graphics.FromImage(oBitmap)
            oGraphic.Clear(Color.White)
            oGraphic.DrawString(ex.Message, _
                    New System.Drawing.Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Point), _
                    Brushes.Black, 1, 1)
            oBitmap.Save(Context.Response.OutputStream, ImageFormat.Jpeg)
        End Try

    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return True
        End Get
    End Property

End Class

Clase: NaturalHelp.vb

 

Imports System.Text
Imports System.Drawing

Friend Class NaturalHelp

    ”’ <summary>
    ”’ Encrypt a string value to base 64
    ”’ </summary>
    ”’ <param name="strText">Text to encrypt</param>
    ”’ <returns>Base 64 string value</returns>
    ”’ <remarks></remarks>
    Public Function EncryptToBase64(ByVal strText As String) As String
        Dim _btyEncoded As Byte() = Encoding.ASCII.GetBytes(strText)
        Return Convert.ToBase64String(_btyEncoded)
    End Function

    ”’ <summary>
    ”’ Decrypt a base 64 string to string
    ”’ </summary>
    ”’ <param name="strText">Base 64 string to decrypt</param>
    ”’ <returns>Decrypted string</returns>
    ”’ <remarks></remarks>
    Public Function DecryptToBase64(ByVal strText As String) As String
        Dim _btyDecoded As Byte() = Convert.FromBase64String(strText)
        Dim _oByteDecoder As New System.Text.ASCIIEncoding

        Return _oByteDecoder.GetString(_btyDecoded)
    End Function

End Class



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