1. How to randomly generate images
Generate the System. Drawing. Bitmap object, and use System. Drawing. Graphics to draw to the Bitmap object.
2. How to pass image data through parameters in WebService Methods
Outputs Bitmap objects into word throttling, and WebMothod returns the byte stream using Byte arrays.
Instance:
1. Use VS. NET 2003 to create an ASP. NET Webservice project. The default Service name is MyService. Add a WebMethod named GenerateVerifyImage to MyService. The code for this method is as follows:
/// <Summary> 〉
/// Generate the image Verification Code
/// </Summary> 〉
/// <Param name = "nLen"> Verification code length </param> 〉
/// <Param name = "strKey"> output parameter, verification code content </param> 〉
/// <Returns> image 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 background color
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. Fill in the 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 interference lines with a color that is slightly darker 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 );
}
// Use the character set, which can be expanded immediately and control the probability of occurrence of Characters
String strCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 4. cyclically obtain characters 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 the font
System. Drawing. Font font = new System. Drawing. Font ("Courier New",
12 + rd. Next () % 4,
System. Drawing. FontStyle. Bold );
Char c = strCode [rd. Next (strCode. Length)]; // random character acquisition
StrResult + = c. ToString ();
// Draw 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;
}
2. Test WebMethod, add a WebForm, reference the preceding WebService, and reference the name imagesvr. Add code to Page_Load:
...
Imagesvr. MyService imgsvr = new imagesvr. MyService ();
String strKey = "";
Byte [] data = imgsvr. GenerateVerifyImage (5, ref strKey );
Response. OutputStream. Write (data, 0, data. Length );
...
3. Run. Each time the WebForm is refresh, a newly generated image verification code is displayed, and the output parameter strKey of the function stores the actual content of the verification code, which can be saved in the Session, used for verification.