473,703 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating Random Image Authorization

I am trying to set up an Image authorization where you type in the value
that is in a picture to log on to our site.

I found a program that is supposed to do it, but it doesn't seem to work.

It should put a blue and yellow box on the page with "This is a test" as
part of the picture. But what I get is a broken Gif.

The other problem is that I can't view the source???? The view source is
disabled for this page. What causes this?

Am I missing something (I'm sure I am)?

Here is the code.

*************** *************** *************** *************** *************** ***
<%@ Page Language="VB" trace="false" Debug="true" Explicit="True"
Buffer="True"%>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Drawing" %>
<html><head><ti tle>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Sourc e As Object, E As EventArgs)
Dim pageContext As System.Web.Http Context

pageContext = System.Web.Http Context.Current

'//Reading the parameter

Dim strText As String = Request("param" )

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFo rmat

pixFormat = Imaging.PixelFo rmat.Format32bp pArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromIm age(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Colo r.Yellow)

'//Creating a font object with Arial size 10 and bold

'//public Font(string, float, FontStyle)

Dim objFont As Font = New Font("Arial", 10, FontStyle.Bold)

'//Set the background half rectangle blue

'//Method Signature: FillRectangle (const Brush *brush, INT x, INT y, INT
width, INT height);

graphicsObject. FillRectangle(N ew SolidBrush(Colo r.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject. DrawString(strT ext, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject. Flush()

'//Setting the response header content MIME type

pageContext.Res ponse.ContentTy pe = "image/gif"

'//Saving the file to output stream to be displayed in browser

raster.Save(pag eContext.Respon se.OutputStream , Imaging.ImageFo rmat.Gif)

'//Flushing to the Response

pageContext.Res ponse.Flush()
End Sub

</script>
<body bgcolor="#FFFFF F" topmargin="0" marginheight="0 "
onLoad="documen t.forms[0].Test.focus();" >
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButto n" Text="Submit It" runat="server" />
</form></center></body></html>
*************** *************** *************** *************** ************

Thanks,

Tom

Jun 15 '06 #1
4 2752
Here are a couple of CAPTCHA components that should satisfy your
requirements:
http://www.15seconds.com/issue/040204.htm
http://www.codeproject.com/aspnet/CaptchaControl.asp
http://www.devx.com/dotnet/Article/21308

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OA******** ******@TK2MSFTN GP05.phx.gbl...
I am trying to set up an Image authorization where you type in the value
that is in a picture to log on to our site.

I found a program that is supposed to do it, but it doesn't seem to work.

It should put a blue and yellow box on the page with "This is a test" as
part of the picture. But what I get is a broken Gif.

The other problem is that I can't view the source???? The view source is
disabled for this page. What causes this?

Am I missing something (I'm sure I am)?

Here is the code.

*************** *************** *************** *************** *************** ***
<%@ Page Language="VB" trace="false" Debug="true" Explicit="True"
Buffer="True"%>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Drawing" %>
<html><head><ti tle>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Sourc e As Object, E As EventArgs)
Dim pageContext As System.Web.Http Context

pageContext = System.Web.Http Context.Current

'//Reading the parameter

Dim strText As String = Request("param" )

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFo rmat

pixFormat = Imaging.PixelFo rmat.Format32bp pArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromIm age(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Colo r.Yellow)

'//Creating a font object with Arial size 10 and bold

'//public Font(string, float, FontStyle)

Dim objFont As Font = New Font("Arial", 10, FontStyle.Bold)

'//Set the background half rectangle blue

'//Method Signature: FillRectangle (const Brush *brush, INT x, INT y, INT
width, INT height);

graphicsObject. FillRectangle(N ew SolidBrush(Colo r.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject. DrawString(strT ext, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject. Flush()

'//Setting the response header content MIME type

pageContext.Res ponse.ContentTy pe = "image/gif"

'//Saving the file to output stream to be displayed in browser

raster.Save(pag eContext.Respon se.OutputStream , Imaging.ImageFo rmat.Gif)

'//Flushing to the Response

pageContext.Res ponse.Flush()
End Sub

</script>
<body bgcolor="#FFFFF F" topmargin="0" marginheight="0 "
onLoad="documen t.forms[0].Test.focus();" >
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButto n" Text="Submit It" runat="server" />
</form></center></body></html>
*************** *************** *************** *************** ************

Thanks,

Tom

Jun 15 '06 #2
I am going to look at those files also, as I am not sure how I am going to
do it yet.

But I can't figure out why the program won't display the image.

I found another program that works fine if I create it as a VS project, but
I needed to take it out of the project for a Dreamweaver project I have and
it does the same thing. Shows a broken gif. The code is identical (except
it is not using code behind).

The aspx file is in the root and the class is in the bin folder. The code
is really straightforward and simple. Captcha class creates the image and
the jpegImage.aspx just calls it and then displays the image.

jpegImage.aspx
*************** *************** ***************
<%@ Page language="c#" trace="false" debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<%@ Import Namespace="MyFu nctions" %>
<%@ Import Namespace="Syst em.Drawing.Imag ing" %>
<script runat="server">
private void Page_Load(objec t sender, System.EventArg s e)
{
// Create a CAPTCHA image using the text stored in the Session object.
string imageText;
if (this.Session["CaptchaImageTe xt"] == null)
imageText = "12345";
else
imageText = this.Session["CaptchaImageTe xt"].ToString();
MyFunctions.Cap tchaImage ci = new MyFunctions.Cap tchaImage(image Text,
200, 50, "Century Schoolbook");

// Change the response headers to output a JPEG image.
this.Response.C lear();
this.Response.C ontentType = "image/jpeg";

// Write the image to the response stream in JPEG format.
ci.Image.Save(t his.Response.Ou tputStream, ImageFormat.Jpe g);

// Dispose of the CAPTCHA image object.
ci.Dispose();
}
</script>
<html>
<head>
<title>JpegImag e</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body>
<form id="JpegImage" method="post" runat="server">
</form>
</body>
</html>
*************** *************** ***************

Captcha.cs
*************** *************** *************** ****
using System;
using System.Drawing;
using System.Drawing. Drawing2D;
using System.Drawing. Imaging;
using System.Drawing. Text;

namespace MyFunctions
{
/// <summary>
/// Summary description for CaptchaImage.
/// </summary>
public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}

// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;

// For generating random numbers.
private Random random = new Random();

// =============== =============== =============== =============== ========
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// =============== =============== =============== =============== ========
public CaptchaImage(st ring s, int width, int height)
{
this.text = s;
this.SetDimensi ons(width, height);
this.GenerateIm age();
}

// =============== =============== =============== =============== ========
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// =============== =============== =============== =============== ========
public CaptchaImage(st ring s, int width, int height, string familyName)
{
this.text = s;
this.SetDimensi ons(width, height);
this.SetFamilyN ame(familyName) ;
this.GenerateIm age();
}

// =============== =============== =============== =============== ========
// This member overrides Object.Finalize .
// =============== =============== =============== =============== ========
~CaptchaImage()
{
Dispose(false);
}

// =============== =============== =============== =============== ========
// Releases all resources used by this object.
// =============== =============== =============== =============== ========
public void Dispose()
{
GC.SuppressFina lize(this);
this.Dispose(tr ue);
}

// =============== =============== =============== =============== ========
// Custom Dispose method to clean up unmanaged resources.
// =============== =============== =============== =============== ========
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Disp ose();
}

// =============== =============== =============== =============== ========
// Sets the image width and height.
// =============== =============== =============== =============== ========
private void SetDimensions(i nt width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRa ngeException("w idth", width, "Argument out of
range, must be greater than zero.");
if (width <= 0)
throw new ArgumentOutOfRa ngeException("h eight", height, "Argument out of
range, must be greater than zero.");
this.width = width;
this.height = height;
}

// =============== =============== =============== =============== ========
// Sets the font used for the image text.
// =============== =============== =============== =============== ========
private void SetFamilyName(s tring familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.famil yName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing. FontFamily.Gene ricSerif.Name;
}
}

// =============== =============== =============== =============== ========
// Creates the bitmap image.
// =============== =============== =============== =============== ========
private void GenerateImage()
{
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.wid th, this.height,
PixelFormat.For mat32bppArgb);

// Create a graphics object for drawing.
Graphics g = Graphics.FromIm age(bitmap);
g.SmoothingMode = SmoothingMode.A ntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(Hatc hStyle.SmallCon fetti,
Color.LightGray , Color.White);
g.FillRectangle (hatchBrush, rect);

// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.famil yName, fontSize, FontStyle.Bold) ;
size = g.MeasureString (this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignmen t = StringAlignment .Center;
format.LineAlig nment = StringAlignment .Center;

// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString( this.text, font.FontFamily , (int) font.Style, font.Size,
rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.ran dom.Next(rect.W idth) / v,
this.random.Nex t(rect.Height) / v),
new PointF(rect.Wid th - this.random.Nex t(rect.Width) / v,
this.random.Nex t(rect.Height) / v),
new PointF(this.ran dom.Next(rect.W idth) / v, rect.Height -
this.random.Nex t(rect.Height) / v),
new PointF(rect.Wid th - this.random.Nex t(rect.Width) / v, rect.Height -
this.random.Nex t(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translat e(0F, 0F);
path.Warp(point s, rect, matrix, WarpMode.Perspe ctive, 0F);

// Draw the text.
hatchBrush = new HatchBrush(Hatc hStyle.LargeCon fetti, Color.LightGray ,
Color.DarkGray) ;
g.FillPath(hatc hBrush, path);

// Add some random noise.
int m = Math.Max(rect.W idth, rect.Height);
for (int i = 0; i < (int) (rect.Width * rect.Height / 30F); i++)
{
int x = this.random.Nex t(rect.Width);
int y = this.random.Nex t(rect.Height);
int w = this.random.Nex t(m / 50);
int h = this.random.Nex t(m / 50);
g.FillEllipse(h atchBrush, x, y, w, h);
}

// Clean up.
font.Dispose();
hatchBrush.Disp ose();
g.Dispose();

// Set the image.
this.image = bitmap;
}
}
}************** *************** *************** ******

Why won't the image display?

Thanks,

Tom

"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:un******** ******@TK2MSFTN GP05.phx.gbl...
Here are a couple of CAPTCHA components that should satisfy your
requirements:
http://www.15seconds.com/issue/040204.htm
http://www.codeproject.com/aspnet/CaptchaControl.asp
http://www.devx.com/dotnet/Article/21308

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OA******** ******@TK2MSFTN GP05.phx.gbl...
I am trying to set up an Image authorization where you type in the value
that is in a picture to log on to our site.

I found a program that is supposed to do it, but it doesn't seem to work.

It should put a blue and yellow box on the page with "This is a test" as
part of the picture. But what I get is a broken Gif.

The other problem is that I can't view the source???? The view source is
disabled for this page. What causes this?

Am I missing something (I'm sure I am)?

Here is the code.

*************** *************** *************** *************** *************** ***
<%@ Page Language="VB" trace="false" Debug="true" Explicit="True"
Buffer="True"%>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Drawing" %>
<html><head><ti tle>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Sourc e As Object, E As EventArgs)
Dim pageContext As System.Web.Http Context

pageContext = System.Web.Http Context.Current

'//Reading the parameter

Dim strText As String = Request("param" )

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFo rmat

pixFormat = Imaging.PixelFo rmat.Format32bp pArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromIm age(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Colo r.Yellow)

'//Creating a font object with Arial size 10 and bold

'//public Font(string, float, FontStyle)

Dim objFont As Font = New Font("Arial", 10, FontStyle.Bold)

'//Set the background half rectangle blue

'//Method Signature: FillRectangle (const Brush *brush, INT x, INT y, INT
width, INT height);

graphicsObject. FillRectangle(N ew SolidBrush(Colo r.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject. DrawString(strT ext, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject. Flush()

'//Setting the response header content MIME type

pageContext.Res ponse.ContentTy pe = "image/gif"

'//Saving the file to output stream to be displayed in browser

raster.Save(pag eContext.Respon se.OutputStream , Imaging.ImageFo rmat.Gif)

'//Flushing to the Response

pageContext.Res ponse.Flush()
End Sub

</script>
<body bgcolor="#FFFFF F" topmargin="0" marginheight="0 "
onLoad="documen t.forms[0].Test.focus();" >
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButto n" Text="Submit It" runat="server" />
</form></center></body></html>
*************** *************** *************** *************** ************

Thanks,

Tom


Jun 15 '06 #3
The only difference I can see is that the CodeBehind JpegImage.aspx has the
following line:

<%@ Page language="c#" Codebehind="Jpe gImage.aspx.cs"
AutoEventWireup ="false" Inherits="Captc haImage.JpegIma ge" %>

There is an 'Inherits="Capt chaImage.JpegIm age"' in it, but I don't know what
I would need to add to my single page to make it work.

Thanks,

Tom
"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** *******@TK2MSFT NGP04.phx.gbl.. .
I am going to look at those files also, as I am not sure how I am going to
do it yet.

But I can't figure out why the program won't display the image.

I found another program that works fine if I create it as a VS project,
but I needed to take it out of the project for a Dreamweaver project I
have and it does the same thing. Shows a broken gif. The code is
identical (except it is not using code behind).

The aspx file is in the root and the class is in the bin folder. The code
is really straightforward and simple. Captcha class creates the image and
the jpegImage.aspx just calls it and then displays the image.

jpegImage.aspx
*************** *************** ***************
<%@ Page language="c#" trace="false" debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<%@ Import Namespace="MyFu nctions" %>
<%@ Import Namespace="Syst em.Drawing.Imag ing" %>
<script runat="server">
private void Page_Load(objec t sender, System.EventArg s e)
{
// Create a CAPTCHA image using the text stored in the Session object.
string imageText;
if (this.Session["CaptchaImageTe xt"] == null)
imageText = "12345";
else
imageText = this.Session["CaptchaImageTe xt"].ToString();
MyFunctions.Cap tchaImage ci = new MyFunctions.Cap tchaImage(image Text,
200, 50, "Century Schoolbook");

// Change the response headers to output a JPEG image.
this.Response.C lear();
this.Response.C ontentType = "image/jpeg";

// Write the image to the response stream in JPEG format.
ci.Image.Save(t his.Response.Ou tputStream, ImageFormat.Jpe g);

// Dispose of the CAPTCHA image object.
ci.Dispose();
}
</script>
<html>
<head>
<title>JpegImag e</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body>
<form id="JpegImage" method="post" runat="server">
</form>
</body>
</html>
*************** *************** ***************

Captcha.cs
*************** *************** *************** ****
using System;
using System.Drawing;
using System.Drawing. Drawing2D;
using System.Drawing. Imaging;
using System.Drawing. Text;

namespace MyFunctions
{
/// <summary>
/// Summary description for CaptchaImage.
/// </summary>
public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}

// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;

// For generating random numbers.
private Random random = new Random();

// =============== =============== =============== =============== ========
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// =============== =============== =============== =============== ========
public CaptchaImage(st ring s, int width, int height)
{
this.text = s;
this.SetDimensi ons(width, height);
this.GenerateIm age();
}

// =============== =============== =============== =============== ========
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// =============== =============== =============== =============== ========
public CaptchaImage(st ring s, int width, int height, string familyName)
{
this.text = s;
this.SetDimensi ons(width, height);
this.SetFamilyN ame(familyName) ;
this.GenerateIm age();
}

// =============== =============== =============== =============== ========
// This member overrides Object.Finalize .
// =============== =============== =============== =============== ========
~CaptchaImage()
{
Dispose(false);
}

// =============== =============== =============== =============== ========
// Releases all resources used by this object.
// =============== =============== =============== =============== ========
public void Dispose()
{
GC.SuppressFina lize(this);
this.Dispose(tr ue);
}

// =============== =============== =============== =============== ========
// Custom Dispose method to clean up unmanaged resources.
// =============== =============== =============== =============== ========
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Disp ose();
}

// =============== =============== =============== =============== ========
// Sets the image width and height.
// =============== =============== =============== =============== ========
private void SetDimensions(i nt width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRa ngeException("w idth", width, "Argument out of
range, must be greater than zero.");
if (width <= 0)
throw new ArgumentOutOfRa ngeException("h eight", height, "Argument out
of range, must be greater than zero.");
this.width = width;
this.height = height;
}

// =============== =============== =============== =============== ========
// Sets the font used for the image text.
// =============== =============== =============== =============== ========
private void SetFamilyName(s tring familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.famil yName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing. FontFamily.Gene ricSerif.Name;
}
}

// =============== =============== =============== =============== ========
// Creates the bitmap image.
// =============== =============== =============== =============== ========
private void GenerateImage()
{
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.wid th, this.height,
PixelFormat.For mat32bppArgb);

// Create a graphics object for drawing.
Graphics g = Graphics.FromIm age(bitmap);
g.SmoothingMode = SmoothingMode.A ntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(Hatc hStyle.SmallCon fetti,
Color.LightGray , Color.White);
g.FillRectangle (hatchBrush, rect);

// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.famil yName, fontSize, FontStyle.Bold) ;
size = g.MeasureString (this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignmen t = StringAlignment .Center;
format.LineAlig nment = StringAlignment .Center;

// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString( this.text, font.FontFamily , (int) font.Style, font.Size,
rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.ran dom.Next(rect.W idth) / v,
this.random.Nex t(rect.Height) / v),
new PointF(rect.Wid th - this.random.Nex t(rect.Width) / v,
this.random.Nex t(rect.Height) / v),
new PointF(this.ran dom.Next(rect.W idth) / v, rect.Height -
this.random.Nex t(rect.Height) / v),
new PointF(rect.Wid th - this.random.Nex t(rect.Width) / v, rect.Height -
this.random.Nex t(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translat e(0F, 0F);
path.Warp(point s, rect, matrix, WarpMode.Perspe ctive, 0F);

// Draw the text.
hatchBrush = new HatchBrush(Hatc hStyle.LargeCon fetti, Color.LightGray ,
Color.DarkGray) ;
g.FillPath(hatc hBrush, path);

// Add some random noise.
int m = Math.Max(rect.W idth, rect.Height);
for (int i = 0; i < (int) (rect.Width * rect.Height / 30F); i++)
{
int x = this.random.Nex t(rect.Width);
int y = this.random.Nex t(rect.Height);
int w = this.random.Nex t(m / 50);
int h = this.random.Nex t(m / 50);
g.FillEllipse(h atchBrush, x, y, w, h);
}

// Clean up.
font.Dispose();
hatchBrush.Disp ose();
g.Dispose();

// Set the image.
this.image = bitmap;
}
}
}************** *************** *************** ******

Why won't the image display?

Thanks,

Tom

"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:un******** ******@TK2MSFTN GP05.phx.gbl...
Here are a couple of CAPTCHA components that should satisfy your
requirements:
http://www.15seconds.com/issue/040204.htm
http://www.codeproject.com/aspnet/CaptchaControl.asp
http://www.devx.com/dotnet/Article/21308

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OA******** ******@TK2MSFTN GP05.phx.gbl...
I am trying to set up an Image authorization where you type in the value
that is in a picture to log on to our site.

I found a program that is supposed to do it, but it doesn't seem to
work.

It should put a blue and yellow box on the page with "This is a test" as
part of the picture. But what I get is a broken Gif.

The other problem is that I can't view the source???? The view source
is disabled for this page. What causes this?

Am I missing something (I'm sure I am)?

Here is the code.

*************** *************** *************** *************** *************** ***
<%@ Page Language="VB" trace="false" Debug="true" Explicit="True"
Buffer="True"%>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Drawing" %>
<html><head><ti tle>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Sourc e As Object, E As EventArgs)
Dim pageContext As System.Web.Http Context

pageContext = System.Web.Http Context.Current

'//Reading the parameter

Dim strText As String = Request("param" )

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFo rmat

pixFormat = Imaging.PixelFo rmat.Format32bp pArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromIm age(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Colo r.Yellow)

'//Creating a font object with Arial size 10 and bold

'//public Font(string, float, FontStyle)

Dim objFont As Font = New Font("Arial", 10, FontStyle.Bold)

'//Set the background half rectangle blue

'//Method Signature: FillRectangle (const Brush *brush, INT x, INT y,
INT width, INT height);

graphicsObject. FillRectangle(N ew SolidBrush(Colo r.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject. DrawString(strT ext, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject. Flush()

'//Setting the response header content MIME type

pageContext.Res ponse.ContentTy pe = "image/gif"

'//Saving the file to output stream to be displayed in browser

raster.Save(pag eContext.Respon se.OutputStream , Imaging.ImageFo rmat.Gif)

'//Flushing to the Response

pageContext.Res ponse.Flush()
End Sub

</script>
<body bgcolor="#FFFFF F" topmargin="0" marginheight="0 "
onLoad="documen t.forms[0].Test.focus();" >
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButto n" Text="Submit It" runat="server" />
</form></center></body></html>
*************** *************** *************** *************** ************

Thanks,

Tom



Jun 15 '06 #4
I found the problem, I'm just not sure why.

I have a problem that handles my Scrolling on Page Back and puts the page
back where it left off.

It uses an HTTPModule:

<httpModules>
<add
type="NFission. WebControls.Scr ollKeeperModule ,NFission.WebCo ntrols.ScrollKe eper"
name="ScollKeep erModule" />
</httpModules>

This module catches the page and changes the scroll position of the page in
the module before the page is handled:

*************** *************** ************
app.Context.Ite ms["scrollKeeper.A ddScroll"] = doPage;
app.Context.Ite ms["scrollKeeper.D oScroll"] = false;
app.Context.Ite ms["scrollKeeper.s crollX"] = 0;
app.Context.Ite ms["scrollKeeper.s crollY"] = 0;

SKFilter filter = new SKFilter(hApp.R esponse.Filter) ;
hApp.Response.F ilter = filter;
*************** *************** **************

This is using the Response class, but it is done before the page is handled.
This is done than my page is done where I build the image and then give it
to the Response object:

pageContext.Res ponse.ContentTy pe = "image/gif"
'//Saving the file to output stream to be displayed in browser
raster.Save(pag eContext.Respon se.OutputStream , Imaging.ImageFo rmat.Gif)
'//Flushing to the Response
pageContext.Res ponse.Flush()

This seems to get lost. I could understand it if it happened before
Scrollkeeper got it, but why afterwards?

Thanks,

Tom
"tshad" <ts**********@f tsolutions.com> wrote in message
news:Oj******** ******@TK2MSFTN GP02.phx.gbl...
The only difference I can see is that the CodeBehind JpegImage.aspx has
the following line:

<%@ Page language="c#" Codebehind="Jpe gImage.aspx.cs"
AutoEventWireup ="false" Inherits="Captc haImage.JpegIma ge" %>

There is an 'Inherits="Capt chaImage.JpegIm age"' in it, but I don't know
what I would need to add to my single page to make it work.

Thanks,

Tom
"tshad" <ts**********@f tsolutions.com> wrote in message
news:%2******** *******@TK2MSFT NGP04.phx.gbl.. .
I am going to look at those files also, as I am not sure how I am going to
do it yet.

But I can't figure out why the program won't display the image.

I found another program that works fine if I create it as a VS project,
but I needed to take it out of the project for a Dreamweaver project I
have and it does the same thing. Shows a broken gif. The code is
identical (except it is not using code behind).

The aspx file is in the root and the class is in the bin folder. The
code is really straightforward and simple. Captcha class creates the
image and the jpegImage.aspx just calls it and then displays the image.

jpegImage.aspx
*************** *************** ***************
<%@ Page language="c#" trace="false" debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<%@ Import Namespace="MyFu nctions" %>
<%@ Import Namespace="Syst em.Drawing.Imag ing" %>
<script runat="server">
private void Page_Load(objec t sender, System.EventArg s e)
{
// Create a CAPTCHA image using the text stored in the Session object.
string imageText;
if (this.Session["CaptchaImageTe xt"] == null)
imageText = "12345";
else
imageText = this.Session["CaptchaImageTe xt"].ToString();
MyFunctions.Cap tchaImage ci = new MyFunctions.Cap tchaImage(image Text,
200, 50, "Century Schoolbook");

// Change the response headers to output a JPEG image.
this.Response.C lear();
this.Response.C ontentType = "image/jpeg";

// Write the image to the response stream in JPEG format.
ci.Image.Save(t his.Response.Ou tputStream, ImageFormat.Jpe g);

// Dispose of the CAPTCHA image object.
ci.Dispose();
}
</script>
<html>
<head>
<title>JpegImag e</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body>
<form id="JpegImage" method="post" runat="server">
</form>
</body>
</html>
*************** *************** ***************

Captcha.cs
*************** *************** *************** ****
using System;
using System.Drawing;
using System.Drawing. Drawing2D;
using System.Drawing. Imaging;
using System.Drawing. Text;

namespace MyFunctions
{
/// <summary>
/// Summary description for CaptchaImage.
/// </summary>
public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}

// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;

// For generating random numbers.
private Random random = new Random();

// =============== =============== =============== =============== ========
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// =============== =============== =============== =============== ========
public CaptchaImage(st ring s, int width, int height)
{
this.text = s;
this.SetDimensi ons(width, height);
this.GenerateIm age();
}

// =============== =============== =============== =============== ========
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// =============== =============== =============== =============== ========
public CaptchaImage(st ring s, int width, int height, string familyName)
{
this.text = s;
this.SetDimensi ons(width, height);
this.SetFamilyN ame(familyName) ;
this.GenerateIm age();
}

// =============== =============== =============== =============== ========
// This member overrides Object.Finalize .
// =============== =============== =============== =============== ========
~CaptchaImage()
{
Dispose(false);
}

// =============== =============== =============== =============== ========
// Releases all resources used by this object.
// =============== =============== =============== =============== ========
public void Dispose()
{
GC.SuppressFina lize(this);
this.Dispose(tr ue);
}

// =============== =============== =============== =============== ========
// Custom Dispose method to clean up unmanaged resources.
// =============== =============== =============== =============== ========
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Disp ose();
}

// =============== =============== =============== =============== ========
// Sets the image width and height.
// =============== =============== =============== =============== ========
private void SetDimensions(i nt width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRa ngeException("w idth", width, "Argument out of
range, must be greater than zero.");
if (width <= 0)
throw new ArgumentOutOfRa ngeException("h eight", height, "Argument out
of range, must be greater than zero.");
this.width = width;
this.height = height;
}

// =============== =============== =============== =============== ========
// Sets the font used for the image text.
// =============== =============== =============== =============== ========
private void SetFamilyName(s tring familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.famil yName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing. FontFamily.Gene ricSerif.Name;
}
}

// =============== =============== =============== =============== ========
// Creates the bitmap image.
// =============== =============== =============== =============== ========
private void GenerateImage()
{
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.wid th, this.height,
PixelFormat.For mat32bppArgb);

// Create a graphics object for drawing.
Graphics g = Graphics.FromIm age(bitmap);
g.SmoothingMode = SmoothingMode.A ntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(Hatc hStyle.SmallCon fetti,
Color.LightGray , Color.White);
g.FillRectangle (hatchBrush, rect);

// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.famil yName, fontSize, FontStyle.Bold) ;
size = g.MeasureString (this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignmen t = StringAlignment .Center;
format.LineAlig nment = StringAlignment .Center;

// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString( this.text, font.FontFamily , (int) font.Style, font.Size,
rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.ran dom.Next(rect.W idth) / v,
this.random.Nex t(rect.Height) / v),
new PointF(rect.Wid th - this.random.Nex t(rect.Width) / v,
this.random.Nex t(rect.Height) / v),
new PointF(this.ran dom.Next(rect.W idth) / v, rect.Height -
this.random.Nex t(rect.Height) / v),
new PointF(rect.Wid th - this.random.Nex t(rect.Width) / v,
rect.Height - this.random.Nex t(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translat e(0F, 0F);
path.Warp(point s, rect, matrix, WarpMode.Perspe ctive, 0F);

// Draw the text.
hatchBrush = new HatchBrush(Hatc hStyle.LargeCon fetti, Color.LightGray ,
Color.DarkGray) ;
g.FillPath(hatc hBrush, path);

// Add some random noise.
int m = Math.Max(rect.W idth, rect.Height);
for (int i = 0; i < (int) (rect.Width * rect.Height / 30F); i++)
{
int x = this.random.Nex t(rect.Width);
int y = this.random.Nex t(rect.Height);
int w = this.random.Nex t(m / 50);
int h = this.random.Nex t(m / 50);
g.FillEllipse(h atchBrush, x, y, w, h);
}

// Clean up.
font.Dispose();
hatchBrush.Disp ose();
g.Dispose();

// Set the image.
this.image = bitmap;
}
}
}************** *************** *************** ******

Why won't the image display?

Thanks,

Tom

"Steve C. Orr [MVP, MCSD]" <St***@Orr.ne t> wrote in message
news:un******** ******@TK2MSFTN GP05.phx.gbl...
Here are a couple of CAPTCHA components that should satisfy your
requirements:
http://www.15seconds.com/issue/040204.htm
http://www.codeproject.com/aspnet/CaptchaControl.asp
http://www.devx.com/dotnet/Article/21308

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"tshad" <ts**********@f tsolutions.com> wrote in message
news:OA******** ******@TK2MSFTN GP05.phx.gbl...
I am trying to set up an Image authorization where you type in the value
that is in a picture to log on to our site.

I found a program that is supposed to do it, but it doesn't seem to
work.

It should put a blue and yellow box on the page with "This is a test"
as part of the picture. But what I get is a broken Gif.

The other problem is that I can't view the source???? The view source
is disabled for this page. What causes this?

Am I missing something (I'm sure I am)?

Here is the code.

*************** *************** *************** *************** *************** ***
<%@ Page Language="VB" trace="false" Debug="true" Explicit="True"
Buffer="True"%>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Data.SqlClie nt" %>
<%@ Import Namespace="Syst em.Drawing" %>
<html><head><ti tle>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Sourc e As Object, E As EventArgs)
Dim pageContext As System.Web.Http Context

pageContext = System.Web.Http Context.Current

'//Reading the parameter

Dim strText As String = Request("param" )

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFo rmat

pixFormat = Imaging.PixelFo rmat.Format32bp pArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromIm age(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Colo r.Yellow)

'//Creating a font object with Arial size 10 and bold

'//public Font(string, float, FontStyle)

Dim objFont As Font = New Font("Arial", 10, FontStyle.Bold)

'//Set the background half rectangle blue

'//Method Signature: FillRectangle (const Brush *brush, INT x, INT y,
INT width, INT height);

graphicsObject. FillRectangle(N ew SolidBrush(Colo r.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject. DrawString(strT ext, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject. Flush()

'//Setting the response header content MIME type

pageContext.Res ponse.ContentTy pe = "image/gif"

'//Saving the file to output stream to be displayed in browser

raster.Save(pag eContext.Respon se.OutputStream , Imaging.ImageFo rmat.Gif)

'//Flushing to the Response

pageContext.Res ponse.Flush()
End Sub

</script>
<body bgcolor="#FFFFF F" topmargin="0" marginheight="0 "
onLoad="documen t.forms[0].Test.focus();" >
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButto n" Text="Submit It" runat="server" />
</form></center></body></html>
*************** *************** *************** *************** ************

Thanks,

Tom




Jun 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
9700
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to call that in another page so that i get a random image the page is http://2006ymcanationals.com/index.php using <img
6
3531
by: Olly | last post by:
I've found a basic script, however I also need to add alt and title attributes as well, how would I go about doing this? Here's the script I found: Thanks <script language="JavaScript"> <!-- /*
2
2224
by: Andrea | last post by:
I'm having some difficulty creating a report in Access and I need some suggestions. My company issues "Return Authorizations" when customers need to return products. A customer calls in and we provide the number. The customer then returns items for a refund or for an exchange. Here is the basic set up of the tables in question. -ReturnAuthorizationTable- ReturnNumber_PK CustomerID ReturnType
5
2189
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets details of a random image from an access database. I know this works because when I run the query in access, it picks a different image each time (or close enough). However, when I view the .aspx page that this code-behind is for, it always picks...
6
3073
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional) file path to store image resource object contents * @see actual_path */
12
5226
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
2
3011
by: dgk | last post by:
I have an image in the masterpage which works fine everywhere except the login page. I have the login control on a page (login.aspx naturally) and the user is directed to it via forms authentication. On that page, the image in the masterpage is replaced by the default square. Anyone know what's up with this?
4
3080
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random import mimetools import StringIO
3
1399
by: dieselfuelonly | last post by:
<?php //Connect to the database and select the data from the table $username="asdfasdf"; $password="asdfasdf"; $database="asdfasdf"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database"); $query="SELECT * FROM ut_players"; $result=mysql_query($query);
0
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3123
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2069
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.