public static NCaptcha GenerateAndDraw(byte showCount, System.IO.Stream outStream, EnImage enImg, string SetSessionName) { NCaptcha code = new NCaptcha(showCount); code.SetSessionName = SetSessionName; code.IsSetSession = true; EnFont font = new EnFont(); font.MyFont = enImg.ImgFont; font.MyColor = enImg.ImgColor; EnImage img = new EnImage(); img.ImgColor = Color.White; ; img.Width = enImg.Width; img.Height = enImg.Height; code.gBitmap = code.gDrawing(EnImage.PaintShape.Link, img, font); if (code.gBitmap != null && outStream != null) { code.gBitmap.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg); } return(code); }
private Bitmap gDrawing(EnImage.PaintShape ps, EnImage enImg, EnFont font) { Bitmap gBitmap = new Bitmap(enImg.Width, enImg.Height); Graphics gp = Graphics.FromImage(gBitmap); gp.Clear(enImg.ImgColor); if (!string.IsNullOrEmpty(this.GenerateString)) { List <EnFont> listFont = new List <EnFont>(); for (int i = 0; i < this.GenerateString.Length; i++) { EnFont enFont = new EnFont(); float y = float.Parse("" + ((enImg.Height / 2 - font.MyFont.Size / 2))); //RSeed.Next((int)font.MyFont.Size, gBitmap.Height - (int)(font.MyFont.Size)); float x = i * font.MyFont.Size; enFont.MyColor = EnObject.listColor[0]; SolidBrush shadowBrush = new SolidBrush(font.MyColor); y = RSeed.Next((int)y, (int)font.MyFont.Size / 2); enFont.MyPointF = new PointF(x, y); listFont.Add(enFont); gp.DrawString(this.GenerateString.Substring(i, 1), font.MyFont, shadowBrush, enFont.MyPointF); } switch (ps) { case EnImage.PaintShape.Link: gp.DrawLine(new Pen(font.MyColor, 1), 0, RSeed.Next((int)(enImg.Height / 2), (int)(enImg.Height * 0.55)), enImg.Width, RSeed.Next((int)(enImg.Height / 3), (int)(enImg.Height * 0.8))); gp.DrawLine(new Pen(font.MyColor, 1), 0, RSeed.Next((int)(enImg.Height / 2), (int)(enImg.Height * 0.50)), enImg.Width, RSeed.Next((int)(enImg.Height / 3), (int)(enImg.Height * 0.82))); break; case EnImage.PaintShape.Dot: break; case EnImage.PaintShape.Ring: break; default: break; } gp.Save(); } return(gBitmap); }
/// <summary> /// 生成随机字符串并生成图片 /// </summary> /// <param name="showCount">字符串长度</param> /// <param name="outStream">输出流,可以为NULL</param> /// <param name="iformat">图片类型</param> /// <param name="isSetSession">是否保存Session中</param> /// <returns>返回对象</returns> public static NCaptcha GenerateAndDraw(byte showCount, System.IO.Stream outStream, System.Drawing.Imaging.ImageFormat iformat, bool isSetSession) { NCaptcha code = new NCaptcha(showCount); code.IsSetSession = isSetSession; EnFont font = new EnFont(); font.MyFont = new Font(FontFamily.GenericSerif, 20, FontStyle.Bold, GraphicsUnit.Point); font.MyColor = EnObject.listColor[0]; EnImage img = new EnImage(); img.ImgColor = Color.White; ; img.Width = code.GenerateCount * (int)font.MyFont.Size; img.Height = (int)(font.MyFont.Size) + 5; code.gBitmap = code.gDrawing(EnImage.PaintShape.Link, img, font); if (code.gBitmap != null && outStream != null) { code.gBitmap.Save(outStream, iformat); } return(code); }