labs.insert-title.com

GDI+ Security String image

The Code: verify.aspx

	
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim r as New Random
Dim baseMap as Bitmap = new Bitmap(200, 50)
Dim myGraphic as Graphics = Graphics.FromImage(baseMap)
Dim HBrush As HatchBrush = New HatchBrush( RandomHatch(), GetRandomColor(r))
Dim upBrush as SolidBrush = new SolidBrush(GetRandomColor(r))
Dim fontTitle as Font = new Font("tahoma", 24)
myGraphic.FillRectangle(HBrush, 0, 0, 200, 50)  


myGraphic.DrawString(RandomStringGenerator(6), fontTitle, upBrush, r.Next(0, 80), r.Next(0, 10))
Response.ContentType = "image/jpeg"
baseMap.Save(Response.OutputStream, ImageFormat.JPEG)
myGraphic.Dispose()
baseMap.Dispose()
End Sub

Public Function RandomStringGenerator(ByVal intLen As Integer) As String
    Dim rRandom As New Random()
    Dim Zero As Integer = Asc("0")
    Dim Nine As Integer  = Asc("9")
    Dim capA As Integer  = Asc("A")
    Dim capZ As Integer  = Asc("Z")
    Dim litA As Integer  = Asc("a")
    Dim litZ As Integer  = Asc("z")    
    
Dim iRandNum as integer
	Dim strTemp as String = ""
	Dim i As Integer
	While i < intLen
	iRandNum = rRandom.Next(Zero, litZ)
		If ((((iRandNum > = Zero) And (iRandNum <= Nine) Or _
		(iRandNum > = capA) And (iRandNum < = capZ) Or _
		(iRandNum > = litA) And (iRandNum < = litZ)))) Then 
			strTemp = strTemp + Chr(iRandNum) 
			i+=1
	       End If
	End while
	Return strTemp
End Function

Public Function RandomHatch() As HatchStyle 
	Randomize() 
	Select Case Rnd() * 56 
		Case Is < 1 : Return HatchStyle.BackwardDiagonal
		Case Is < 2 : Return HatchStyle.Cross
		Case Is < 3 : Return HatchStyle.DarkDownwardDiagonal
		Case Is < 4 : Return HatchStyle.DarkHorizontal
		Case Is < 5 : Return HatchStyle.DarkUpwardDiagonal
		Case Is < 6 : Return HatchStyle.DarkVertical
		Case Is < 7 : Return HatchStyle.DashedDownwardDiagonal
		Case Is < 8 : Return HatchStyle.DashedHorizontal
		Case Is < 9 : Return HatchStyle.DashedUpwardDiagonal
		Case Is < 10 : Return HatchStyle.DashedVertical
		Case Is < 11 : Return HatchStyle.DiagonalBrick
		Case Is < 12 : Return HatchStyle.DiagonalCross
		Case Is < 13 : Return HatchStyle.Divot
		Case Is < 14 : Return HatchStyle.DottedDiamond
		Case Is < 15 : Return HatchStyle.ForwardDiagonal
		Case Is < 16 : Return HatchStyle.Horizontal
		Case Is < 17 : Return HatchStyle.HorizontalBrick
		Case Is < 18 : Return HatchStyle.LargeCheckerBoard
		Case Is < 19 : Return HatchStyle.LargeConfetti
		Case Is < 20 : Return HatchStyle.LargeGrid
		Case Is < 21 : Return HatchStyle.LightDownwardDiagonal  
		Case Is < 22 : Return HatchStyle.LightHorizontal
		Case Is < 23 : Return HatchStyle.LightUpwardDiagonal
		Case Is < 24 : Return HatchStyle.LightVertical
		Case Is < 25 : Return HatchStyle.DottedGrid
		Case Is < 26 : Return HatchStyle.Max
		Case Is < 27 : Return HatchStyle.Min
		Case Is < 28 : Return HatchStyle.NarrowHorizontal
		Case Is < 29 : Return HatchStyle.NarrowVertical
		Case Is < 30 : Return HatchStyle.OutlinedDiamond
		Case Is < 31 : Return HatchStyle.Percent05
		Case Is < 32 : Return HatchStyle.Percent10
		Case Is < 33 : Return HatchStyle.Percent20
		Case Is < 34 : Return HatchStyle.Percent25
		Case Is < 35 : Return HatchStyle.Percent30
		Case Is < 36 : Return HatchStyle.Percent40
		Case Is < 37 : Return HatchStyle.Percent50
		Case Is < 38 : Return HatchStyle.Percent60
		Case Is < 39 : Return HatchStyle.Percent70
		Case Is < 40 : Return HatchStyle.Percent75
		Case Is < 41 : Return HatchStyle.Percent80
		Case Is < 42 : Return HatchStyle.Percent90
		Case Is < 43 : Return HatchStyle.Plaid
		Case Is < 44 : Return HatchStyle.Shingle
		Case Is < 45 : Return HatchStyle.SmallCheckerBoard
		Case Is < 46 : Return HatchStyle.SmallConfetti
		Case Is < 47 : Return HatchStyle.SmallGrid
		Case Is < 48 : Return HatchStyle.SolidDiamond
		Case Is < 49 : Return HatchStyle.Sphere
		Case Is < 50 : Return HatchStyle.ZigZag
		Case Is < 51 : Return HatchStyle.Trellis
		Case Is < 52 : Return HatchStyle.Vertical
		Case Is < 53 : Return HatchStyle.Wave
		Case Is < 54 : Return HatchStyle.Weave
		Case Is < 55 : Return HatchStyle.WideDownwardDiagonal
		Case Is < 56 : Return HatchStyle.WideUpwardDiagonal
	End Select 
End Function 


Private Function GetRandomColor(r) As Color
    Return Color.FromArgb(r.Next(0, 256), r.Next(0, 256), r.Next(0,256))
End Function
</script>

Usage


<img src="verify.aspx" />
	

Real Time Example






Discussion

kajanth

hi this code is vey neat. i need to develop something similar for my uni project and was wondering if you could give me some tips and how i can use this to block automation bots.

kajan

mkajanth@hotmail.com

Joe Maddalone

Pass strTemp to a public variable. Use a form / input field / submit button. Make sure the value entered in the field matches the strTemp value. DONE.

smith288

The random colors can be hard to see from time to time with weird fore/back combos of blue and black or even black and black.

Joe Maddalone

Repeat this mantra: "That`s a feature, not a bug, a feature." "That`s a feature, not a bug, a feature." "That`s a feature, not a bug, a feature."

Rahul Dev Katarey

its cool, but can do this with simple asp ".asp" ? i Just Want to know

Terry

Joe: I placed an image Webcontrol on a form. How would you save your image to an existing image or placeholder. Thanks

Joe Maddalone

Terry, sounds simple enough... I am sure when I get the chance to look into your question it should not be too hard.

David

Hi joe Can i show this image in an image control?

Joe Maddalone

I dont see why not.

New comments are currently disabled, you can find me @joemaddalone Comments temporarily disabled for now, you can find me @joemaddalone