473,287 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
<html><head><title>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim pageContext As System.Web.HttpContext

pageContext = System.Web.HttpContext.Current

'//Reading the parameter

Dim strText As String = Request("param")

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFormat

pixFormat = Imaging.PixelFormat.Format32bppArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromImage(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Color.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(New SolidBrush(Color.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject.DrawString(strText, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject.Flush()

'//Setting the response header content MIME type

pageContext.Response.ContentType = "image/gif"

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

raster.Save(pageContext.Response.OutputStream, Imaging.ImageFormat.Gif)

'//Flushing to the Response

pageContext.Response.Flush()
End Sub

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

Thanks,

Tom

Jun 15 '06 #1
4 2716
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**********@ftsolutions.com> wrote in message
news:OA**************@TK2MSFTNGP05.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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
<html><head><title>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim pageContext As System.Web.HttpContext

pageContext = System.Web.HttpContext.Current

'//Reading the parameter

Dim strText As String = Request("param")

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFormat

pixFormat = Imaging.PixelFormat.Format32bppArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromImage(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Color.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(New SolidBrush(Color.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject.DrawString(strText, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject.Flush()

'//Setting the response header content MIME type

pageContext.Response.ContentType = "image/gif"

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

raster.Save(pageContext.Response.OutputStream, Imaging.ImageFormat.Gif)

'//Flushing to the Response

pageContext.Response.Flush()
End Sub

</script>
<body bgcolor="#FFFFFF" topmargin="0" marginheight="0"
onLoad="document.forms[0].Test.focus();">
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButton" 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="MyFunctions" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
// Create a CAPTCHA image using the text stored in the Session object.
string imageText;
if (this.Session["CaptchaImageText"] == null)
imageText = "12345";
else
imageText = this.Session["CaptchaImageText"].ToString();
MyFunctions.CaptchaImage ci = new MyFunctions.CaptchaImage(imageText,
200, 50, "Century Schoolbook");

// Change the response headers to output a JPEG image.
this.Response.Clear();
this.Response.ContentType = "image/jpeg";

// Write the image to the response stream in JPEG format.
ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);

// Dispose of the CAPTCHA image object.
ci.Dispose();
}
</script>
<html>
<head>
<title>JpegImage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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(string s, int width, int height)
{
this.text = s;
this.SetDimensions(width, height);
this.GenerateImage();
}

// ================================================== ==================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ================================================== ==================
public CaptchaImage(string s, int width, int height, string familyName)
{
this.text = s;
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}

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

// ================================================== ==================
// Releases all resources used by this object.
// ================================================== ==================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}

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

// ================================================== ==================
// Sets the image width and height.
// ================================================== ==================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of
range, must be greater than zero.");
if (width <= 0)
throw new ArgumentOutOfRangeException("height", 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(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}

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

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

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti,
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.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = 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.random.Next(rect.Width) / v,
this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v,
this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height -
this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height -
this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray,
Color.DarkGray);
g.FillPath(hatchBrush, path);

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

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

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

Why won't the image display?

Thanks,

Tom

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:un**************@TK2MSFTNGP05.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**********@ftsolutions.com> wrote in message
news:OA**************@TK2MSFTNGP05.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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
<html><head><title>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim pageContext As System.Web.HttpContext

pageContext = System.Web.HttpContext.Current

'//Reading the parameter

Dim strText As String = Request("param")

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFormat

pixFormat = Imaging.PixelFormat.Format32bppArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromImage(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Color.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(New SolidBrush(Color.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject.DrawString(strText, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject.Flush()

'//Setting the response header content MIME type

pageContext.Response.ContentType = "image/gif"

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

raster.Save(pageContext.Response.OutputStream, Imaging.ImageFormat.Gif)

'//Flushing to the Response

pageContext.Response.Flush()
End Sub

</script>
<body bgcolor="#FFFFFF" topmargin="0" marginheight="0"
onLoad="document.forms[0].Test.focus();">
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButton" 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="JpegImage.aspx.cs"
AutoEventWireup="false" Inherits="CaptchaImage.JpegImage" %>

There is an 'Inherits="CaptchaImage.JpegImage"' 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**********@ftsolutions.com> wrote in message
news:%2***************@TK2MSFTNGP04.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="MyFunctions" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
// Create a CAPTCHA image using the text stored in the Session object.
string imageText;
if (this.Session["CaptchaImageText"] == null)
imageText = "12345";
else
imageText = this.Session["CaptchaImageText"].ToString();
MyFunctions.CaptchaImage ci = new MyFunctions.CaptchaImage(imageText,
200, 50, "Century Schoolbook");

// Change the response headers to output a JPEG image.
this.Response.Clear();
this.Response.ContentType = "image/jpeg";

// Write the image to the response stream in JPEG format.
ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);

// Dispose of the CAPTCHA image object.
ci.Dispose();
}
</script>
<html>
<head>
<title>JpegImage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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(string s, int width, int height)
{
this.text = s;
this.SetDimensions(width, height);
this.GenerateImage();
}

// ================================================== ==================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ================================================== ==================
public CaptchaImage(string s, int width, int height, string familyName)
{
this.text = s;
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}

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

// ================================================== ==================
// Releases all resources used by this object.
// ================================================== ==================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}

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

// ================================================== ==================
// Sets the image width and height.
// ================================================== ==================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of
range, must be greater than zero.");
if (width <= 0)
throw new ArgumentOutOfRangeException("height", 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(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}

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

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

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti,
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.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = 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.random.Next(rect.Width) / v,
this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v,
this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height -
this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height -
this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray,
Color.DarkGray);
g.FillPath(hatchBrush, path);

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

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

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

Why won't the image display?

Thanks,

Tom

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:un**************@TK2MSFTNGP05.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**********@ftsolutions.com> wrote in message
news:OA**************@TK2MSFTNGP05.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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
<html><head><title>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim pageContext As System.Web.HttpContext

pageContext = System.Web.HttpContext.Current

'//Reading the parameter

Dim strText As String = Request("param")

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFormat

pixFormat = Imaging.PixelFormat.Format32bppArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromImage(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Color.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(New SolidBrush(Color.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject.DrawString(strText, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject.Flush()

'//Setting the response header content MIME type

pageContext.Response.ContentType = "image/gif"

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

raster.Save(pageContext.Response.OutputStream, Imaging.ImageFormat.Gif)

'//Flushing to the Response

pageContext.Response.Flush()
End Sub

</script>
<body bgcolor="#FFFFFF" topmargin="0" marginheight="0"
onLoad="document.forms[0].Test.focus();">
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButton" 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.ScrollKeeperModule,NFis sion.WebControls.ScrollKeeper"
name="ScollKeeperModule" />
</httpModules>

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

******************************************
app.Context.Items["scrollKeeper.AddScroll"] = doPage;
app.Context.Items["scrollKeeper.DoScroll"] = false;
app.Context.Items["scrollKeeper.scrollX"] = 0;
app.Context.Items["scrollKeeper.scrollY"] = 0;

SKFilter filter = new SKFilter(hApp.Response.Filter);
hApp.Response.Filter = 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.Response.ContentType = "image/gif"
'//Saving the file to output stream to be displayed in browser
raster.Save(pageContext.Response.OutputStream, Imaging.ImageFormat.Gif)
'//Flushing to the Response
pageContext.Response.Flush()

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

Thanks,

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

<%@ Page language="c#" Codebehind="JpegImage.aspx.cs"
AutoEventWireup="false" Inherits="CaptchaImage.JpegImage" %>

There is an 'Inherits="CaptchaImage.JpegImage"' 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**********@ftsolutions.com> wrote in message
news:%2***************@TK2MSFTNGP04.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="MyFunctions" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
// Create a CAPTCHA image using the text stored in the Session object.
string imageText;
if (this.Session["CaptchaImageText"] == null)
imageText = "12345";
else
imageText = this.Session["CaptchaImageText"].ToString();
MyFunctions.CaptchaImage ci = new MyFunctions.CaptchaImage(imageText,
200, 50, "Century Schoolbook");

// Change the response headers to output a JPEG image.
this.Response.Clear();
this.Response.ContentType = "image/jpeg";

// Write the image to the response stream in JPEG format.
ci.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);

// Dispose of the CAPTCHA image object.
ci.Dispose();
}
</script>
<html>
<head>
<title>JpegImage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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(string s, int width, int height)
{
this.text = s;
this.SetDimensions(width, height);
this.GenerateImage();
}

// ================================================== ==================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ================================================== ==================
public CaptchaImage(string s, int width, int height, string familyName)
{
this.text = s;
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}

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

// ================================================== ==================
// Releases all resources used by this object.
// ================================================== ==================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}

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

// ================================================== ==================
// Sets the image width and height.
// ================================================== ==================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of
range, must be greater than zero.");
if (width <= 0)
throw new ArgumentOutOfRangeException("height", 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(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}

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

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

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti,
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.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = 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.random.Next(rect.Width) / v,
this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v,
this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height -
this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v,
rect.Height - this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray,
Color.DarkGray);
g.FillPath(hatchBrush, path);

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

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

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

Why won't the image display?

Thanks,

Tom

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:un**************@TK2MSFTNGP05.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**********@ftsolutions.com> wrote in message
news:OA**************@TK2MSFTNGP05.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="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Drawing" %>
<html><head><title>Drilldown Datagrid Searching Within
Results</title></head>
<script runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
Dim pageContext As System.Web.HttpContext

pageContext = System.Web.HttpContext.Current

'//Reading the parameter

Dim strText As String = Request("param")

'//Create the memory map

Dim raster As Bitmap

Dim pixFormat As Imaging.PixelFormat

pixFormat = Imaging.PixelFormat.Format32bppArgb

'// Create a memory image with dimensions 110x50

raster = New Bitmap(110, 50, pixFormat)

Dim graphicsObject As Graphics

graphicsObject = Graphics.FromImage(raster)

'// Instantiate object of brush

Dim objBrush As SolidBrush = New SolidBrush(Color.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(New SolidBrush(Color.Blue), 0, 0, 55, 50)

'//Writing the string on image

graphicsObject.DrawString(strText, objFont, objBrush, 0, 0)

'//Flushing the graphics object

graphicsObject.Flush()

'//Setting the response header content MIME type

pageContext.Response.ContentType = "image/gif"

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

raster.Save(pageContext.Response.OutputStream, Imaging.ImageFormat.Gif)

'//Flushing to the Response

pageContext.Response.Flush()
End Sub

</script>
<body bgcolor="#FFFFFF" topmargin="0" marginheight="0"
onLoad="document.forms[0].Test.focus();">
<center>
<h3>Regular Expression Test</h3>
<form runat="server">
<asp:Button ID="SubmitButton" 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
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...
6
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
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...
5
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...
6
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)...
12
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...
2
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...
4
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...
3
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);...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.