Ok i have build a Composite Web Control
Composite Control(MSDN)It works for the most part. Only one issue with it.
When i run my asp.net application and right click on the image it shows me the filename of the image, but it says that the Address(URL) is unavailable even though i've supplied it the full path to that image.
csharp
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
namespace Captcha
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MultipleControl runat=server></{0}:MultipleControl>")]
public class MultipleControl : CompositeControl, INamingContainer
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
protected System.Web.UI.WebControls.Image captcha = new System.Web.UI.WebControls.Image();
protected TextBox captchatextbox = new TextBox();
protected LinkButton refresh = new LinkButton();
protected override void CreateChildControls()
{
this.Controls.Add(captcha);
this.Controls.Add(refresh);
this.Controls.Add(captchatextbox);
refresh.Click += new EventHandler(this.refresh_Click);
base.CreateChildControls();
}
protected override void OnInit(EventArgs e)
{
captcha.ID = "imgCaptcha";
captchatextbox.ID = "txtCaptcha";
refresh.ID = "CAPTCHArefresh";
refresh.Text = "Click to get new code";
refresh.CausesValidation = false;
BuildImage();
CleanImageFolder();
DisplayImage();
base.OnInit(e);
EnsureChildControls();
}
String URL;
String imageurl;
String IMAGE;
[Description("Where you want to save the image")]
public string savelocation
{
get
{
EnsureChildControls();
return URL;
}
set
{
EnsureChildControls();
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("Arial", 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);
}
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("captchastr", captchastr);
//HttpContext.Current.Session.Add("captchastr", captchastr);
/*MemoryStream ms = new MemoryStream();
Context.Response.Clear();
Context.Response.ContentType = "image/jpeg";
captchabmp.Save(ms, ImageFormat.Jpeg);
ms.WriteTo(Context.Response.OutputStream);*/
captchabmp.Dispose();
return IMAGE;
}
protected void DisplayImage()
{
captcha.ImageUrl = MapPathSecure("~/CAPTCHA/" + IMAGE);
}
protected void CleanImageFolder()
{
string[] imgList = Directory.GetFiles(MapPathSecure("~/CAPTCHA/", "*.jpg"));
foreach (string img in imgList)
{
FileInfo imgInfo = new FileInfo(img);
if (imgInfo.LastWriteTime < DateTime.Now.AddMinutes(-3))
{
imgInfo.Delete();
}
}
}
protected override void Render(HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}
protected void refresh_Click(Object sender, EventArgs e)
{
BuildImage();
DisplayImage();
captchatextbox.Text = "";
}
}
}
the code was sniped of alot of properties so you dont have to scroll through ungodly amounts of code to get to the relavant stuff.
and yes i know its saved as .Gif I have tried saving it as both .Gif and .Jpg and none will reopen.
This post has been edited by rgfirefly24: 13 Jun, 2008 - 07:57 AM