Welcome to Dream.In.Code
Become an Expert!

Join 150,042 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,605 people online right now. Registration is fast and FREE... Join Now!




Composite Web Control

 
Reply to this topicStart new topic

Composite Web Control, working... cept 1 thing

rgfirefly24
13 Jun, 2008 - 07:41 AM
Post #1

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
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.
Attached Image

This post has been edited by rgfirefly24: 13 Jun, 2008 - 07:57 AM
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: Composite Web Control
14 Jun, 2008 - 08:39 AM
Post #2

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
I'm guessing here... Did you set the property "savelocation" with an HTTP value? Meaning you set it to something like "http://someurl.com/" (as opposed to something like "c:\somefolder").

Unless the website allows write access to that particular folder, the image creation wouldn't work.

If I'm wrong, does the correct image show up? If you right-click on it, and do a save image, do you download the correct image?

You might also want to check out the HTML rendered. Does the "src" attribute come out with "c:\somefolder" or "http://someurl.com/"? If it's the c drive version, you might want to do some further customisation in the Render() method (or RenderControl() or the RenderBeginTag()/RenderEndTag() method pair).

You will then set the ImageUrl property as the c drive version, but render the HTML with the HTTP version.

Hope that made sense.
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Composite Web Control
14 Jun, 2008 - 12:26 PM
Post #3

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
QUOTE(orcasquall @ 14 Jun, 2008 - 09:39 AM) *

I'm guessing here... Did you set the property "savelocation" with an HTTP value? Meaning you set it to something like "http://someurl.com/" (as opposed to something like "c:\somefolder").

Unless the website allows write access to that particular folder, the image creation wouldn't work.

If I'm wrong, does the correct image show up? If you right-click on it, and do a save image, do you download the correct image?

You might also want to check out the HTML rendered. Does the "src" attribute come out with "c:\somefolder" or "http://someurl.com/"? If it's the c drive version, you might want to do some further customisation in the Render() method (or RenderControl() or the RenderBeginTag()/RenderEndTag() method pair).

You will then set the ImageUrl property as the c drive version, but render the HTML with the HTTP version.

Hope that made sense.


hrm, it does. The image writes to the savelocation properly, its calling it back from the C:\etc\etc location that is the problem. I'll try using the HTTP render to get it working smile.gif



EDIT: by virtue of Virtual path i got it working biggrin.gif instead of using SecureServerMap i decided to just set

csharp

captcha.ImageUrl = "~\\CAPTCHA\\" + Image;


that worked perfectly. Now on to disabling the ability to download the image.

This post has been edited by rgfirefly24: 16 Jun, 2008 - 07:39 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month