ok guys its clunky but it works lol. Still have a little issue though
maybe you guys can help me with a solution or just tell me it cant be
done either way.
anyway, I can display the image and if the
user types in the captcha it will validate, however since i had to do a
if (!Page.IsPostBack) in order for the captcha text not to override
itself upon every Async post back it now wont refresh the image if
someone clicks get new image.
is there a way i can make this Unaffected by anything causing a post back except my label button?
The Control shown below is being placed onto my Default.aspx page. and the lblbutton is named lbtnCaptchaRefresh.
if
i remember right i cant cross name spaces in a custom control since the
name space of the current project will never be the same between two
projects.
I've also thought about using it as an embedded resources, but not sure how that would work on dynamically created images.
here is the code:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Net;
-
- namespace Captcha
- {
- [DefaultProperty("Text")]
- [ToolboxData("<{0}:Captcha runat=server></{0}:Captcha>")]
- public class Captcha : WebControl
- {
-
- [Bindable(true)]
- [Category("Appearance")]
- [DefaultValue("")]
- [Localizable(true)]
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public override Unit Width
- {
- get
- {
- return base.Width;
- }
- set
- {
- base.Width = 125;
- }
- }
-
-
- public override Unit Height
- {
- get
- {
- return base.Height;
- }
- set
- {
- base.Height = 125;
- }
- }
- int length = 5;
- [Description("The Length of the CAPTCHA Text")]
- public int CAPTCHALength
- {
- get
- {
- return length;
- }
- set
- {
- length = value;
- }
- }
- String backgroundstring = "";
- public string setbackgroundstring
- {
- get
- {
- if (String.IsNullOrEmpty(backgroundstring))
- {
- backgroundstring = "-OCR-";
- return backgroundstring;
- }
- else
- {
- return backgroundstring;
- }
- }
- set
- {
- backgroundstring = value;
- }
- }
- int CAPTCHAheight = 50;
- public int captchaheight
- {
- get
- {
- return CAPTCHAheight;
- }
- set
- {
- CAPTCHAheight = value;
- }
- }
- int CAPTCHAwidth = 150;
- public int captchawidth
- {
- get
- {
- return CAPTCHAwidth;
- }
- set
- {
- CAPTCHAwidth = value;
- }
- }
- int FONTSIZE = 40;
- public int fontsize
- {
- get
- {
- return FONTSIZE;
- }
- set
- {
- FONTSIZE = value;
- }
- }
- Color CAPTCHAFOREGROUNDCOLOR = Color.Gold;
- public Color captchaforegroundcolor
- {
- get
- {
- return CAPTCHAFOREGROUNDCOLOR;
- }
- set
- {
- CAPTCHAFOREGROUNDCOLOR = value;
- }
- }
- Color CAPTCHABACKGROUNDCOLOR = Color.Black;
- public Color captchabackgroundcolor
- {
- get
- {
- return CAPTCHABACKGROUNDCOLOR;
- }
- set
- {
- CAPTCHABACKGROUNDCOLOR = value;
- }
- }
- Color TEXTCOLOR1 = Color.GhostWhite;
- public Color primarytextcolor
- {
- get
- {
- return TEXTCOLOR1;
- }
- set
- {
- TEXTCOLOR1 = value;
- }
- }
- Color TEXTCOLOR2 = Color.Gold;
- public Color secondarytextcolor
- {
- get
- {
- return TEXTCOLOR2;
- }
- set
- {
- TEXTCOLOR2 = value;
- }
- }
- HatchStyle BACKGROUNDHATCHTYPE = HatchStyle.SmallConfetti;
- public HatchStyle setBackgroundHatchStyle
- {
- get
- {
- return BACKGROUNDHATCHTYPE;
- }
- set
- {
- BACKGROUNDHATCHTYPE = value;
- }
- }
- HatchStyle TEXTHACHTYPE = HatchStyle.Shingle;
- public HatchStyle setTextHatchStyle
- {
- get
- {
- return TEXTHACHTYPE;
- }
- set
- {
- TEXTHACHTYPE = value;
- }
- }
- String URL; String imageurl;
- [Description("Where you want to save the image")]
- public string savelocation
- {
- get
- {
- return URL;
- }
- set
- {
- URL = value;
- }
- }
- public string BuildImage()
- {
- Bitmap captchabmp = new Bitmap(CAPTCHAwidth, CAPTCHAheight);
- Graphics captchagraphic = Graphics.FromImage(captchabmp);
- Rectangle rect = new Rectangle(0, 0, CAPTCHAwidth, CAPTCHAheight);
- captchagraphic.FillRectangle(new HatchBrush(BACKGROUNDHATCHTYPE, CAPTCHAFOREGROUNDCOLOR, CAPTCHABACKGROUNDCOLOR), rect);
-
- captchagraphic.SmoothingMode = SmoothingMode.AntiAlias;
-
- string captchastr = "";
-
- char[] captchaarray = new char[length];
-
- int x;
-
- Random rand = new Random();
- Random upperlower = new Random();
- Random captcha = new Random();
-
- int z;
- int y;
- string temp;
- for (x = 0; x < length; x++)
- {
- z = captcha.Next(0, 3);
- if (z == 1)
- {
- captchaarray[x] = System.Convert.ToChar(rand.Next(65, 90));
- }
- else
- {
- captchaarray[x] = System.Convert.ToChar(rand.Next(49, 57));
- }
- }
-
- for (x = 0; x < length; x++)
- {
- y = upperlower.Next(0, 99);
- if (y >= 0 || y < 50)
- {
- temp = (captchaarray[x].ToString());
- temp = temp.ToLower();
- captchastr += temp.ToString();
- }
- else
- {
- temp = (captchaarray[x].ToString());
- temp = temp.ToUpper();
- captchastr += temp.ToString();
- }
- }
-
- #region Image Builder
- Random ranpoint = new Random();
- int fontStyle = (int)FontStyle.Italic;
- FontFamily family = new FontFamily("Arial");
- int emSize = FONTSIZE;
- Point origin = new Point(ranpoint.Next(rect.Width / 8), ranpoint.Next(rect.Height / 6));
- StringFormat format = StringFormat.GenericDefault;
-
- GraphicsPath captchapath = new GraphicsPath();
- captchapath.AddString(captchastr, family, fontStyle, emSize, origin, format);
- float floatpoint = 6F;
- PointF[] randompoints =
- {
- new PointF(ranpoint.Next(rect.Width) / floatpoint, ranpoint.Next(rect.Height) / floatpoint),
- new PointF(rect.Width - ranpoint.Next(rect.Width)/floatpoint, ranpoint.Next(rect.Height) / floatpoint),
- new PointF(ranpoint.Next(rect.Width)/ floatpoint, rect.Height - ranpoint.Next(rect.Height) / floatpoint),
- new PointF(rect.Width - ranpoint.Next(rect.Width) / floatpoint, rect.Height - ranpoint.Next(rect.Height) / floatpoint)
- };
- Matrix captchamatrix = new Matrix();
- captchamatrix.Translate(2F, 4F);
- captchapath.Warp(randompoints, rect, captchamatrix, WarpMode.Perspective, 25F);
- Font captchafont2 = new Font("Ariel", FONTSIZE);
-
- captchagraphic.DrawString(backgroundstring, captchafont2, Brushes.Black, (rect.Width / 6), (rect.Height / 6));
- captchagraphic.DrawCurve(new Pen(CAPTCHAFOREGROUNDCOLOR, 2), randompoints);
- HatchBrush captchafont = new HatchBrush(TEXTHACHTYPE, Color.GhostWhite, Color.Gold);
- captchagraphic.FillPath(captchafont, captchapath);
-
- #endregion
-
-
- Random randomurlgenerator = new Random();
- String randomurl = randomurlgenerator.Next(1, 10).ToString();
- for (int i = 0; i < 15; i++)
- {
- randomurl += randomurlgenerator.Next(1, 10);
- }
-
- String IMAGE = randomurl + "CAPTCHA.Jpg";
- imageurl = URL + IMAGE;
-
-
- captchafont.Dispose();
- captchafont2.Dispose();
- captchagraphic.Dispose();
- try
- {
- captchabmp.Save(imageurl, ImageFormat.Jpeg);
- }
- catch (Exception ex)
- {
- Context.Response.Write(ex.ToString());
- }
- HttpContext.Current.Session.Add("CaptchaURL", IMAGE);
- HttpContext.Current.Session.Add("captchastr", captchastr);
-
-
-
-
-
-
-
-
- captchabmp.Dispose();
-
-
- return imageurl;
- }
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- if (!Page.IsPostBack)
- {
- BuildImage();
- }
-
- }
-
- protected override void Render(HtmlTextWriter writer)
- {
-
-
- writer.RenderBeginTag("img src=\"" + imageurl + "\"");
- writer.RenderEndTag();
-
- base.Render(writer);
-
- }
-
-
- }
- }