<%@ webservice
class="captchaWebService" language="c#" %> using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using System.IO; using System.Data.OleDb; using System.Drawing; using System.Drawing.Imaging; ///<summary> /// This is the basic Service for CAPTCHA provision. ///</summary>
[WebService(Namespace="http://axisebusiness.com/webservices/")] publicclass captchaWebService : System.Web.Services.WebService { [WebMethod(Description="Returns a CAPTCHA Image in
Base64-Encoding")] publicbyte[] getCaptcha() { return getBytesFromRaster(Server.MapPath(generateImage())); }
// Returns byte from Image file
publicbyte[] getBytesFromRaster(string filename) { if(File.Exists(filename)) { try { FileStream s =File.OpenRead(filename); byte[]
bytes = newbyte[s.Length]; s.Read(bytes, (int)0,
(int)s.Length); return bytes; } catch(Exception
e) { returnnewbyte[0]; } } else { returnnewbyte[0]; } }
[WebMethod(Description="Generates a CAPTCHA Image and
returns filename")] public String generateImage
() { //Reading the parameter from
session this time String strText = selectWord ();//
= Session("param")
//Create the memory map Bitmap raster; System.Drawing.Imaging.PixelFormat pixFormat =
System.Drawing.Imaging.PixelFormat.Format32bppArgb;
// Select an memory image
from file of 290x80px // in the backgrounds folder
named backX.jpg Graphics graphicsObject; System.Drawing.Image imageObject = System.Drawing.Image.FromFile(Server.MapPath(@"backgrounds\back"
+ new Random().Next(9) + ".jpg")); // Creating the raster image
object raster = new Bitmap
(imageObject); //Creating graphics object graphicsObject = Graphics.FromImage(raster); // Instantiate object of
brush with black color SolidBrush objBrush = new SolidBrush(Color.Black); Font objFont; int a; String myFont, str; //Creating an array for most
readable yet cryptic fonts for OCR's // This is entirely up to
developer's discretion String[] crypticFonts = new String[11];
crypticFonts [0] = "Arial"; crypticFonts [1] = "Verdana"; crypticFonts [2] = "Comic Sans MS"; crypticFonts [3] = "Impact"; crypticFonts [4] = "Haettenschweiler"; crypticFonts [5] = "Lucida Sans Unicode"; crypticFonts [6] = "Garamond"; crypticFonts [7] = "Courier New"; crypticFonts [8] = "Book Antiqua"; crypticFonts [9] = "Arial Narrow"; crypticFonts [10] = "Estrangelo Edessa"; //Loop to write the
characters on image // with different fonts. for (a=0;
a<=strText.Length-1; a++) { myFont = crypticFonts[new Random().Next(a)]; objFont = new Font(myFont, 20, FontStyle.Bold); str = strText.Substring(a, 1); graphicsObject.DrawString(str, objFont, objBrush,
a*20, 35); graphicsObject.Flush(); }
String filename= new Random().Next().ToString() + ".gif"; raster.Save(Server.MapPath(filename),
System.Drawing.Imaging.ImageFormat.Gif); raster.Dispose(); graphicsObject=null; return filename;
} // End of Function //************************************* // Select word web method // Return type String of random
word from dictionary // Dictionary is based on OGDEN's
BASIC ENGLISH //
http://ogden.basic-english.org/basiceng.html [WebMethod(Description="Get an Word from OGDEN's
dictionary")] public String selectWord () {
// The Connection string
referencing the MDB file String ConnectionString =
"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" +
Server.MapPath("dictionary.mdb") + ";";
// Datareader object OleDbDataReader objReader;
// Creating an array of 26
characters (alphabets in dictionary database as columns) char[] columns = newchar[26];
// Adding the column names in
the array // uses the ASCII character
conversion for selecting values // from A- Z for (int a=65; a<65+26; a++) columns[a-65] = (char)a;
// Query String for selecting
a random column from spelling list database String QuerySQL = "SELECT " + columns[(new Random().Next(26))] + " FROM
spellList";
// Opening the
connection OleDbConnection objConn = new OleDbConnection(ConnectionString); // Creating new command
object OleDbCommand objCmd = new OleDbCommand(); // Assigning command text objCmd.CommandText = QuerySQL; // Assigning the connection
to command object connection attribute objCmd.Connection = objConn;
// Instantiating a random
class object Random randomSeed = new Random(); // Creating a random seed
selector int randomSeedSelector=0; // An string character with
maximum capacity for dictionary column String[] selectedIndex = new String[700]; String str = "";
// This code segment opens
the connection and read the dictionary try { objConn.Open(); objReader = objCmd.ExecuteReader(); while (objReader.Read()) { str = objReader.GetValue(0).ToString(); if (str.Length != 0) { selectedIndex[randomSeedSelector]
=str; randomSeedSelector++; } }// Ends While
str = selectedIndex[randomSeed.Next(randomSeedSelector)];
} // Ends Try
catch (Exception Err) { // The Error Catching
operations } finally { objConn.Close(); } // Returns the selected string return (str); }// ends web method
} // Ends Class |