Development of image authentication code service based on ASP.net webservice

Source: Internet
Author: User
Asp.net|web| Verification code recently, work received a task, the development of a page verification code function, access to some information on the web, and combined with the previous knowledge of cartography, to achieve the following solutions. The generated verification code effect is shown in figure:

Issues to resolve:

1. How to generate pictures randomly

Generates the System.Drawing.Bitmap object and uses System.Drawing.Graphics to draw to the bitmap object.

2. How to pass the picture data through the parameter in the WebService method

The bitmap object is exported as a byte stream, and Webmothod is returned by using an array of bytes.

Instance:

1. Create a ASP.net WebService project with Vs.net 2003, the default service name is MyService, and add a myservice named Generateverifyimage to WebMethod. The code for the method is as follows:

<summary>
Generate Picture Verification Code
</summary>
<param name= "Nlen" > Verification Code length </param>
<param name= "strkey" > Output parameters, Verification code content </param>
<returns> Picture byte stream </returns>
[WebMethod]
Public byte[] Generateverifyimage (int nlen,ref string strkey)
{
int nbmpwidth = 13*nlen+5;
int nbmpheight = 25;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap (nbmpwidth,nbmpheight);

1. Generate Random background color
int nred,ngreen,nblue; Ternary color of the background
System.Random rd = new Random ((int) System.DateTime.Now.Ticks);
nRed = Rd. Next (255)%128+128;
Ngreen = Rd. Next (255)%128+128;
nblue = Rd. Next (255)%128+128;

   //2. Populating a bitmap background
   system.drawing.graphics graph = System.Drawing.Graphics.FromImage (BMP);
   graph. FillRectangle (New SolidBrush (System.Drawing.Color.FromArgb (nred,ngreen,nblue))
    ,0
    ,0
    ,nbmpwidth
    ,nbmpheight);


3. Draw a disturbing line with a slightly darker color than the background
int nlines = 3;
System.Drawing.Pen Pen = new System.Drawing.Pen (System.Drawing.Color.FromArgb (NRED-17,NGREEN-17,NBLUE-17), 2);
for (int a =0;a< nlines;a++)
{
int x1 = Rd. Next ()% Nbmpwidth;
int y1 = Rd. Next ()% Nbmpheight;
int x2 = Rd. Next ()% Nbmpwidth;
int y2 = Rd. Next ()% Nbmpheight;
Graph. DrawLine (PEN,X1,Y1,X2,Y2);
}

The character set that you use can be expanded immediately, and you can control the probability of the character appearing
String strcode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

4. Loop to get the character and draw
String strresult = "";
for (int i=0;i<nlen;i++)
{
int x = (i*13 + Rd. Next (3));
int y = Rd. Next (4) + 1;

Determine font
System.Drawing.Font Font = new System.Drawing.Font ("Courier new",
+ Rd. Next ()%4,
System.Drawing.FontStyle.Bold);
char C = strcode[rd.  Next (Strcode.length)]; Random Fetch character
Strresult + + c.tostring ();

Drawing characters
Graph. DrawString (c.tostring (),
Font
New SolidBrush (System.Drawing.Color.FromArgb (nred-60+y*3,ngreen-60+y*3,nblue-40+y*3)),
X
y);
}

5. Output byte stream
System.IO.MemoryStream Bstream = new System.IO.MemoryStream ();
Bmp. Save (Bstream,system.drawing.imaging.imageformat.jpeg);
Bmp. Dispose ();
Graph. Dispose ();

strkey = Strresult;
byte[] Bytereturn = Bstream. ToArray ();
Bstream. Close ();

return bytereturn;
}

[1] [2] Next page



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.