Common generation Code Instance encapsulation:
/// <summary>///Generating a memory bitmap/// </summary>/// <param name= "Code" ></param>/// <returns></returns> Public StaticBitmap GetCode ( out stringCode) { intImgWidth = the; intImgHeight = +; //get random charactersCode = Datetimehelper.getcode_ran (4); //List of colors for verification codes, noise lines, noise pointscolor[] color ={color.black, color.red, Color.Blue, Color.green, Color.orange, Color.brown, Color.brown, Color.DarkBlue}; //font list, for verification code string[] Font = {"Times New Roman","MS Mincho","Book Antiqua","Gungsuh","PMingLiU","Impact" }; Bitmap BMP=NewBitmap (ImgWidth, imgheight); Graphics g=graphics.fromimage (BMP); G.clear (Color.White); Random Rnd=NewRandom (); //Draw Noise Line for(inti =0; I <Ten; i++) { intX1 =Rnd. Next (ImgWidth); intY1 =Rnd. Next (ImgHeight); intx2 =Rnd. Next (ImgWidth); inty2 =Rnd. Next (ImgHeight); Color CLR=Color[rnd. Next (color. Length)]; G.drawline (NewPen (CLR), x1, y1, x2, y2); } //draw a Captcha string for(inti =0; i < code.length; i++) { stringFNT =Font[rnd. Next (font. Length)]; Font ft=NewFont (FNT, -); Color CLR=Color[rnd. Next (color. Length)]; g.DrawString (Code[i]. ToString (), FT,NewSolidBrush (CLR), (floatI +, (float)8); } //Draw Noise points for(inti =0; I < -; i++) { intx =Rnd. Next (BMP. Width); inty =Rnd. Next (BMP. Height); Color CLR=Color[rnd. Next (color. Length)]; Bmp. SetPixel (x, Y, CLR); } //explicitly releasing resources//bmp. Dispose ();G.dispose (); returnbmp;}/// <summary>///generate bitmap, output to response stream/// </summary>/// <param name= "Response" ></param>/// <param name= "Code" ></param> Public Static voidGetCode (Httpresponsebase Response, out stringCode) {Code=string. Empty; Bitmap bit= GetCode ( outCode); ////Clear the page output cache, set the page without caching //Response.Buffer = true; //Response.ExpiresAbsolute = DateTime.Now.AddMilliseconds (0); //Response.Expires = 0; //Response.CacheControl = "No-cache"; //Response.appendheader ("Pragma", "No-cache");response.clearcontent (); Bit. Save (Response.outputstream, imageformat.png); Response.ContentType="Image/png"; //Freeing Resourcesbit. Dispose ();}
C # Generate Authenticode instance