public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor) { VerifyImageInfo verifyImageInfo = new VerifyImageInfo("image/pjpeg", ImageFormat.Jpeg); width = 47; height = 20; Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(image); graphics.SmoothingMode = SmoothingMode.HighSpeed; graphics.Clear(bgcolor); int num = 60; Pen pen = new Pen(Color.FromArgb(VerifyImage.Next(50) + num, VerifyImage.Next(50) + num, VerifyImage.Next(50) + num), 1f); SolidBrush solidBrush = new SolidBrush(Color.FromArgb(VerifyImage.Next(100), VerifyImage.Next(100), VerifyImage.Next(100))); for (int i = 0; i < 3; i++) { graphics.DrawArc(pen, VerifyImage.Next(20) - 10, VerifyImage.Next(20) - 10, VerifyImage.Next(width) + 10, VerifyImage.Next(height) + 10, VerifyImage.Next(-100, 100), VerifyImage.Next(-200, 200)); } for (int j = 0; j < code.Length; j++) { solidBrush = new SolidBrush(Color.Black); PointF point = new PointF((float)j * 7.2f + 4.2f, 2.6f); graphics.DrawString(code[j].ToString(), VerifyImage.font, solidBrush, point); } solidBrush.Dispose(); graphics.Dispose(); verifyImageInfo.Image = image; return(verifyImageInfo); }
/// <summary> /// 生成验证码 /// </summary> /// <param name="code"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="bgcolor"></param> /// <param name="textcolor"></param> /// <returns></returns> public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor, int textcolor) { VerifyImageInfo verifyimage = new VerifyImageInfo(); verifyimage.ImageFormat = ImageFormat.Jpeg; verifyimage.ContentType = "image/pjpeg"; // 直接指定尺寸, 而不使用外部参数中的建议尺寸 width = 120; height = 40; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighSpeed; g.Clear(bgcolor); int fixedNumber = textcolor == 2 ? 60 : 0; Pen linePen = new Pen(Color.FromArgb(Next(50) + fixedNumber, Next(50) + fixedNumber, Next(50) + fixedNumber), 1); SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100))); for (int i = 0; i < 3; i++) { g.DrawArc(linePen, Next(20) - 10, Next(20) - 10, Next(width) + 10, Next(height) + 10, Next(-100, 100), Next(-200, 200)); } Graphics charg = Graphics.FromImage(charbmp); float charx = -18; for (int i = 0; i < code.Length; i++) { m.Reset(); m.RotateAt(Next(50) - 25, new PointF(Next(3) + 7, Next(3) + 7)); charg.Clear(Color.Transparent); charg.Transform = m; //定义前景色为黑色 drawBrush.Color = Color.Black; charx = charx + 18 + Next(5); PointF drawPoint = new PointF(charx, 2.0F); charg.DrawString(code[i].ToString(), fonts[Next(fonts.Length - 1)], drawBrush, new PointF(0, 0)); charg.ResetTransform(); g.DrawImage(charbmp, drawPoint); } drawBrush.Dispose(); g.Dispose(); charg.Dispose(); verifyimage.Image = bitmap; return(verifyimage); }
public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor, int textcolor) { VerifyImageInfo verifyImageInfo = new VerifyImageInfo(); verifyImageInfo.ImageFormat = ImageFormat.Jpeg; verifyImageInfo.ContentType = "image/pjpeg"; width = 120; height = 40; Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(image); graphics.SmoothingMode = SmoothingMode.HighSpeed; graphics.Clear(bgcolor); int num = (textcolor == 2) ? 60 : 0; Pen pen = new Pen(Color.FromArgb(Next(50) + num, Next(50) + num, Next(50) + num), 1f); SolidBrush solidBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100))); for (int i = 0; i < 3; i++) { graphics.DrawArc(pen, Next(20) - 10, Next(20) - 10, Next(width) + 10, Next(height) + 10, Next(-100, 100), Next(-200, 200)); } Graphics graphics2 = Graphics.FromImage(charbmp); float num2 = -18f; for (int j = 0; j < code.Length; j++) { m.Reset(); m.RotateAt((float)(Next(50) - 25), new PointF((float)(Next(3) + 7), (float)(Next(3) + 7))); graphics2.Clear(Color.Transparent); graphics2.Transform = m; solidBrush.Color = Color.Black; num2 = num2 + 18f + (float)Next(5); PointF point = new PointF(num2, 2f); graphics2.DrawString(code[j].ToString(), fonts[Next(fonts.Length - 1)], solidBrush, new PointF(0f, 0f)); graphics2.ResetTransform(); graphics.DrawImage(charbmp, point); } solidBrush.Dispose(); graphics.Dispose(); graphics2.Dispose(); verifyImageInfo.Image = image; return(verifyImageInfo); }
/// <summary> /// 生成验证码 /// </summary> /// <param name="code"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="bgcolor"></param> /// <returns></returns> public VerifyImageInfo GenerateImage(string code, int width, int height, Color bgcolor) { VerifyImageInfo verifyimage = new VerifyImageInfo("image/pjpeg", ImageFormat.Jpeg); //直接指定尺寸, 而不使用外部参数中的建议尺寸 width = 47; height = 20; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.HighSpeed; g.Clear(bgcolor); //画干扰线 int fixedNumber = 60; Pen linePen = new Pen(Color.FromArgb(Next(50) + fixedNumber, Next(50) + fixedNumber, Next(50) + fixedNumber), 1); SolidBrush drawBrush = new SolidBrush(Color.FromArgb(Next(100), Next(100), Next(100))); for (int i = 0; i < 3; i++) { g.DrawArc(linePen, Next(20) - 10, Next(20) - 10, Next(width) + 10, Next(height) + 10, Next(-100, 100), Next(-200, 200)); } //画验证码 for (int i = 0; i < code.Length; i++) { drawBrush = new SolidBrush(Color.Black); PointF point = new PointF((i * 7.2F) + 4.2F, 2.6F); g.DrawString(code[i].ToString(), font, drawBrush, point); } //释放资源 drawBrush.Dispose(); g.Dispose(); verifyimage.Image = bitmap; return(verifyimage); }