|
Hi,
Can anyone help to see what the problem is, with the image not being triggered?
I converted and adapted the code to C#, but the image is not displayed. Thanks
CodeImage.aspx.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CodeImageRandom
{
public partial class CodeImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create a Bitmap image
int x = 155, y = 85;
System.Drawing.Bitmap ImageSrc = new System.Drawing.Bitmap(155, 85);
// Fill it randomly with white pixels
for (int iX = 0; iX <= ImageSrc.Width - 1; iX++)
{
for (int iY = 0; iY <= ImageSrc.Height - 1; iY++)
{
Random Rnd = new Random();
// if (Rnd.Next((int)Math.Pow(10, 6)) > 0.5)
ImageSrc.SetPixel(iX, iY, System.Drawing.Color.White);
}
}
// Create an ImageGraphics Graphics object from bitmap Image
System.Drawing.Graphics ImageGraphics = System.Drawing.Graphics.FromImage(ImageSrc);
// Generate random code with 6 digit.
int NoDigits = 6;
Random rnd = new Random();
string hiddenCode = rnd.Next((int)Math.Pow(10, (NoDigits - 1)), (int)Math.Pow(10, NoDigits) - 1).ToString();
// Set Session variable
Session["hiddenCode"] = hiddenCode;
// Draw random code within Image
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 20, FontStyle.Italic);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//float x = 5.0 + (Math.Pow() / (double)1) * (ImageSrc.Width - 120);
//float y = 5.0 + (Math.rnd() / (double)1) * (ImageSrc.Height - 30);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
ImageGraphics.DrawString(hiddenCode, drawFont, drawBrush, x, y, drawFormat);
// Change reponse content MIME type
Response.ContentType = "image/jpeg";
// Sent Image using Response OutputStream
ImageSrc.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// Dispose Objects used
drawFont.Dispose();
drawBrush.Dispose();
ImageGraphics.Dispose();
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CodeImageRandom.Default" %>
<title>
Enter the Code in image:
<asp:textbox id="TextBox1" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 144px"
="" runat="server">
<asp:button id="Button1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 176px" runat="server"
="" text="Button" width="128px" height="32px">
<asp:label id="Label1" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 224px" runat="server"
="" width="232px" height="32px">
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
namespace CodeImageRandom
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != Session["hiddenCode"].ToString())
Label1.Text = "Wrong Code!";
else
Label1.Text = "Correct Code!";
}
}
}
|
|
|
|
|
Hi,
can you make the code available in C#?
I run the code in VB and it gave error "Could not load type 'CreateImage.Global'."
thanks
|
|
|
|
|
this is really gr8 artical. iam trying to comvert in to c#, but iam facing problem with "Rnd()" for this what should i chang in the code. please can u.....
Pradeep Reddy
|
|
|
|
|
Chill for a few days... I'll post my C# control for this later. If you are just having trouble with the Rand function... post your question in the C# forum. It's pretty easy to use, but you might have a missing library or something.
"Quality Software since 1983!" http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!
|
|
|
|
|
thank u...
Pradeep Reddy
|
|
|
|
|
First create the Random number generator and initialize it with time-dependent seed:
Random Rnd = new Random();
By the way. We should do this in VisualBasic using Randomize().
Now change
Rnd() > 0.5
With:
r.NextDouble() > 0.5
And change
(Fix(Rnd() * 10000000)).ToString
With
r.Next().ToString();
And
5.0 + (Rnd() / 1) * (ImageSrc.Width - 120)
With
r.Next(5,120);
|
|
|
|
|
thank you very much...i got it
Pradeep Reddy
|
|
|
|
|
I followed the suggestions posted here, but the image still doesn't trigger. I'll post the code here, in case anyone can help.
CodeImagem.aspx.cs
namespace CodeImageRandom
{
public partial class CodeImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create a Bitmap image
System.Drawing.Bitmap ImageSrc = new System.Drawing.Bitmap(155, 85);
Random Rnd = new Random();
// Fill it randomly with white pixels
for (int iX = 0; iX <= ImageSrc.Width - 1; iX++)
{
for (int iY = 0; iY <= ImageSrc.Height - 1; iY++)
{
if (Rnd.NextDouble() > 0.5)
ImageSrc.SetPixel(iX, iY, System.Drawing.Color.White);
}
}
// Create an ImageGraphics Graphics object from bitmap Image
System.Drawing.Graphics ImageGraphics = System.Drawing.Graphics.FromImage(ImageSrc);
// Generate random code.
string hiddenCode = (Rnd.Next().ToString());
// Set Session variable
Session["hiddenCode"] = hiddenCode;
// Draw random code within Image
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 20, FontStyle.Italic);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = Rnd.Next(5, 120);
float y = Rnd.Next(5, 30);
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
ImageGraphics.DrawString(hiddenCode, drawFont, drawBrush, x, y, drawFormat);
// Change reponse content MIME type
Response.ContentType = "image/jpeg";
// Sent Image using Response OutputStream
ImageSrc.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
// Dispose Objects used
drawFont.Dispose();
drawBrush.Dispose();
ImageGraphics.Dispose();
}
}
}
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
namespace CodeImageRandom
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != Session["hiddenCode"].ToString())
Label1.Text = "Wrong Code!";
else
Label1.Text = "Correct Code!";
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CodeImageRandom.Default" %>
<title>
Enter the Code in image:
<asp:textbox id="TextBox1" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 144px"
="" runat="server">
<asp:button id="Button1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 176px" runat="server"
="" text="Button" width="128px" height="32px">
<asp:label id="Label1" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 224px" runat="server"
="" width="232px" height="32px">
|
|
|
|
|
Great article but what struck me when I looked at the image at the top of the page is "that's kinda hard to read". I use a solid background (medium blue), on which I draw a few lines (light blue), then I make the letters in black, using a limited set of letters and symbols which are carefully chosen to be easy to recognize. Then I output the whole thing as a GIF. Mine is also done as a user control, so once it's all built, you can simply insert a server tag into the page, and it's all encapsulated nicely with it's own Validator that is capable of invalidating the whole form. I also use an encrypted value which is a HiddenField, so you don't have to use session values, and never have to worry about the plaintext of the verification code being output to the client, which a bot could theoretically read fairly simply. I will be posting an article about that eventually. You can see it in action on this page:
http://www.exit-planner.net/xpra/xpra_forward.aspx[^]
I use C#, so my article will be substantially different from this one, but you could implement the better drawing for your version.
"Quality Software since 1983!" http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!
|
|
|
|
|
Not that I know too much about OCR.
But surly if the robot was to drop the all colours from the image except for black.Obtaining the code would be a trivial task.
I would see how rmortega77`s would not fall to the same simple attack.
And being hard to read should surly be the objective here, at least for an automated system.
|
|
|
|
|
I thought of that, but the thing is... any of these image verifications are going to be subject to that problem. There's a certain point where it becomes so hard to read, that neither OCR nor a human being will be able to decode it. The image posted on the article is pretty close to that level. This system is not designed to defeat an OCR-based attack... and the spammers aren't going to go to that length. There's so many sites that don't use this that it's a waste of time for them to start doing that - they will just make their attack somewhere else. Remember, the power of spammers comes from the ability to hit millions of people in a short time... anything that slows them down is good. It doesn't have to be impossible to beat, it only has to be slightly harder than having nothing at all. It's like the lock on your front door... pretty low-tech security, and isn't going to stop someone who's determined to get in, but it's pretty effective at keeping most people out. When you start to go to extreme lengths, you start keeping your customers out too. If I can't read an image on the first or second try... I'm pretty frustrated. My eyesight is average 20/20, and I'm not color-blind or anything, but I see quite a few of these image verification things that I have trouble with.
I don't know of any research on this, but I would bet that OCR technology is getting pretty close to the human eye at recognising letters.
"Quality Software since 1983!" http://www.smoothjazzy.com/ - see the "Programming" section for (freeware) JazzySiteMaps, a simple application to generate .Net and Google-style sitemaps!
|
|
|
|
|
Jasmine2501 wrote: so hard to read
You can set it to display less random dots.
For iX As Integer = 0 To ImageSrc.Width - 1<br />
For iY As Integer = 0 To ImageSrc.Height - 1<br />
If Rnd() > 0.5 Then<br />
ImageSrc.SetPixel(iX, iY, System.Drawing.Color.White)<br />
End If<br />
Next iY<br />
Next iX
7 years of Web design
|
|
|
|
|
The part where you set pixels to white, which uses SetPixel, could be drastically improved. The problem with SetPixel is that each time it is called, it locks the bitmap in memory, gets the bitmap data, scans to the pixel that needs to be set, changes the pixel value, and unlocks the bitmap from memory.
You can greatly improve the performance of this code by using an unsafe function of a fixed code block to use pointers to set the pixels. When you do this, you lock the bitmap once, change all of the pixels with the one lock and a single incremental seek, and unlock at the end, rather than locking, seeking, changing, unlocking for each pixel.
Take a look at this article for more info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp11152001.asp
|
|
|
|
|
What think about?
' Create a Bitmap image
Dim ImageSrc As System.Drawing.Bitmap = New System.Drawing.Bitmap(1055, 805, Imaging.PixelFormat.Format32bppRgb)
' Get rectangle
Dim ImageSrcRect As New System.Drawing.Rectangle(0, 0, ImageSrc.Width, ImageSrc.Height)
' Set white pixel
Dim WhitePixel As Integer
WhitePixel = -1
' Lock bits
Dim ImageSrcBitmapData As Imaging.BitmapData = _
ImageSrc.LockBits(ImageSrcRect, Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format32bppRgb)
'
Dim PixelSize = Marshal.SizeOf(WhitePixel)
' Fill it randomly with white pixels
Randomize()
For iY As Integer = 0 To (ImageSrc.Height - 1) * ImageSrcBitmapData.Stride Step ImageSrcBitmapData.Stride
For iX As Integer = 0 To ImageSrcBitmapData.Stride - PixelSize Step PixelSize
If Rnd() > 0.5 Then
Marshal.WriteInt32(ImageSrcBitmapData.Scan0, iY + iX, WhitePixel)
End If
Next iX
Next iY
' Unlok Bits
ImageSrc.UnlockBits(ImageSrcBitmapData)
Thanks you
|
|
|
|
|
Amazing! It really in very high performance. Thanks
|
|
|
|
|