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