At present, basically all websites have graphic verification when they log on, adding usernames, passwords, and letters in the pictures before they can log on. I have always wanted to do one, for various reasons, it has been delayed until this afternoon!
In fact, it is not very complicated. We use GDI + to generate an image with a random string, and then display it. That is, space is everywhere. Below is a digital image generated immediately.Code
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. Drawing. imaging;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace webimage
{
/// <Summary>
/// Summary of webform1.
/// </Summary>
Public class webform1: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! Page. ispostback)
{
Showimg ();
}
}
Private void showimg ()
{
This. randomnumber ();
This. drawimage (session ["valdata"]. tostring ());
}
Private void randomnumber ()
{
Random RD = new random ();
String result = "";
For (INT I = 0; I <6; I ++)
{
Result + = RD. Next (10). tostring ();
}
Session ["valdata"] = result;
}
Private void drawimage (string mess)
{
// Create a bitmap object
Bitmap bit = new Bitmap (100,30 );
// A bitmap is available to create a graphics object.
Graphics G = graphics. fromimage (BIT );
Font Ft = new font ("impact", 15 );
// Enter the content
G. drawstring (mess, FT, new solidbrush (color. Yellow), new point (15, 2 ));
// Draw a box
G. drawline (pens. whitesmoke );//-
G. drawline (pens. White, 2,2, 2,28); // |
G. drawline (pens. whitesmoke, 98,2, 98,28 );
G. drawline (pens. White, 2,28, 98,28 );
// Output and release
Bit. Save (response. outputstream, imageformat. GIF );
Bit. Dispose ();
G. Dispose ();
Response. End ();
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}
This generates an image with a random number of six digits. I will take a look at the data and make it later. The following is confusing for me for a long time.
I added a button control on the page and thought about generating a new image once. However, after opening the webpage, there were only random images but no buttons. I thought I had misplaced the page, let's take a look. That's right. Why cannot the button be correctly displayed? I didn't find anything, but I think this is the case. Bit is used for image output. save (response. outputstream, imageformat. GIF); it is the memory stream that throws the button control information. so I want to reference it on another page. Article Use the image control and point the imageurl to that page! No problem!
The following connection is my uploaded code (vs03)
Http://files.cnblogs.com/interboy/webimage.rar