private void Form2_Paint(object sender, PaintEventArgs e) { //패스 그래디언트 Point[] pts = { new Point(100, 0), new Point(0, 100), new Point(200, 100) }; PathGradientBrush B = new PathGradientBrush(pts, WrapMode.Tile); e.Graphics.FillRectangle(B, ClientRectangle); //패스 그래디언트 끝 //패스변형 GraphicsPath Path = new GraphicsPath(); Path.AddString("한글", new FontFamily("궁서"), 0, 100, new Point(10, 30), new StringFormat()); //확장 후 외곽선 그리기 Path.Widen(new Pen(Color.Black, 3)); e.Graphics.DrawPath(Pens.Black, Path); //확장 후 채우기 Path.Widen(new Pen(Color.Blue, 3)); e.Graphics.DrawPath(Pens.Black, Path); //곡선 펴기 Path.Flatten(new Matrix(), 12f); e.Graphics.DrawPath(Pens.Black, Path); //회전 Matrix M = new Matrix(); M.Rotate(-10); Path.Transform(M); e.Graphics.FillPath(Brushes.Blue, Path); //휘기 RectangleF R = Path.GetBounds(); PointF[] arPoint = new PointF[4]; arPoint[0] = new PointF(R.Left, R.Top + 30); arPoint[1] = new PointF(R.Right, R.Top - 10); arPoint[2] = new PointF(R.Left + 10, R.Bottom - 10); arPoint[3] = new PointF(R.Right + 30, R.Bottom + 30); Path.Warp(arPoint, R); e.Graphics.FillPath(Brushes.Blue, Path); //클리핑 //graphicspath path= new graphicspath(); //path.fillmode = fillmode.winding; //path.addellipse(50, 10, 100, 80); //path.addellipse(20, 45, 160, 120); //e.graphics.fillpath(brushes.white, path); //e.graphics.setclip(path); //for (int y = 0; y < bottom; y+= 20) //{ // string str = "눈사람의 모양의클리핑 영역에 글자를 쓴것입니다"; // e.graphics.drawstring(str, font, brushes.blue, 0, y); //} //클리핑 끝 }
public override Bitmap Build(string text) { var random = new Random(); var bitmap = new Bitmap (Width, Height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; var rect = new Rectangle(0, 0, Width, Height); var hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White); g.FillRectangle(hatchBrush, rect); SizeF size; float fontSize = rect.Height + 1; Font font; do { fontSize--; font = new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Bold); size = g.MeasureString(text, font); } while (size.Width > rect.Width); var format = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center}; var path = new GraphicsPath(); //path.AddString(this.text, font.FontFamily, (int) font.Style, // font.Size, rect, format); path.AddString(text, font.FontFamily, (int) font.Style, 75, rect, format); float v = 4F; PointF[] points = { new PointF(random.Next(rect.Width)/v, random.Next( rect.Height)/v), new PointF(rect.Width - random.Next(rect.Width)/v, random.Next(rect.Height)/v), new PointF(random.Next(rect.Width)/v, rect.Height - random.Next(rect.Height)/v), new PointF(rect.Width - random.Next(rect.Width)/v, rect.Height - random.Next(rect.Height)/v) }; var matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); hatchBrush = new HatchBrush(HatchStyle.Percent10, Color.Black, Color.SkyBlue); g.FillPath(hatchBrush, path); int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int) (rect.Width*rect.Height/30F); i++) { int x = random.Next(rect.Width); int y = random.Next(rect.Height); int w = random.Next(m/50); int h = random.Next(m/50); g.FillEllipse(hatchBrush, x, y, w, h); } font.Dispose(); hatchBrush.Dispose(); g.Dispose(); return bitmap; }
private void GenerateImage() { Font font; Bitmap bitmap = new Bitmap(width, height, (PixelFormat)0x26200a); Graphics graphics = Graphics.FromImage(bitmap); graphics.SmoothingMode = (SmoothingMode)4; Rectangle rectangle = new Rectangle(0, 0, width, height); HatchBrush brush = new HatchBrush((HatchStyle)0x22, Color.LightGray, Color.White); graphics.FillRectangle(brush, rectangle); float num = rectangle.Height + 1; do { num--; font = new Font(familyName, num, FontStyle.Bold); } while (graphics.MeasureString(text, font).Width > rectangle.Width); StringFormat format = new StringFormat(); format.Alignment = (StringAlignment)1; format.LineAlignment = (StringAlignment)1; GraphicsPath path = new GraphicsPath(); path.AddString(text, font.FontFamily, (int)font.Style, font.Size, rectangle, format); const float num2 = 4f; PointF[] tfArray = new[] { new PointF((random.Next(rectangle.Width)) / num2,(random.Next(rectangle.Height)) / num2), new PointF(rectangle.Width - ((random.Next(rectangle.Width)) / num2),(random.Next(rectangle.Height) / num2)), new PointF((random.Next(rectangle.Width)) / num2,rectangle.Height - ((random.Next(rectangle.Height)) / num2)), new PointF(rectangle.Width - ((random.Next(rectangle.Width)) / num2),rectangle.Height - ((random.Next(rectangle.Height)) / num2)) }; Matrix matrix = new Matrix(); matrix.Translate(0f, 0f); path.Warp(tfArray, rectangle, matrix, 0, 0f); brush = new HatchBrush((HatchStyle)0x23, Color.LightGray, Color.DarkGray); graphics.FillPath(brush, path); int num3 = Math.Max(rectangle.Width, rectangle.Height); for (int i = 0; i < (((rectangle.Width * rectangle.Height)) / 30f); i++) { int num5 = random.Next(rectangle.Width); int num6 = random.Next(rectangle.Height); int num7 = random.Next(num3 / 50); int num8 = random.Next(num3 / 50); graphics.FillEllipse(brush, num5, num6, num7, num8); } font.Dispose(); brush.Dispose(); graphics.Dispose(); Image = bitmap; }
/// <summary> /// 生成验证码并储存验证码到会话中 /// </summary> public virtual Image Generate(string key, int digits = DefaultDigits, string chars = null) { // 生成验证码 var captchaCode = RandomUtils.RandomString(digits, "23456789ABCDEFGHJKLMNPQRSTUWXYZ"); var image = new Bitmap(digits * 20, 32); var imageRect = new Rectangle(0, 0, image.Width, image.Height); var font = new Font("Arial", 20, FontStyle.Regular); var brush = new SolidBrush(Color.Black); using (var graphic = Graphics.FromImage(image)) { // 描画背景 HatchStyle backgroundStyle = (HatchStyle)RandomUtils.Generator.Next(18, 52); while (!Enum.IsDefined(typeof(HatchStyle), backgroundStyle)) { backgroundStyle = (HatchStyle)RandomUtils.Generator.Next(18, 52); } graphic.FillRectangle( new HatchBrush(backgroundStyle, Color.Gray, Color.White), imageRect); // 描画文本,然后不规则拉伸 GraphicsPath path = new GraphicsPath(); path.AddString(captchaCode, font.FontFamily, (int)font.Style, font.Size, imageRect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }); int padding = 5; Func<int> randomPadding = () => RandomUtils.Generator.Next(padding); var points = new PointF[] { new PointF(randomPadding(), randomPadding()), new PointF(image.Width - randomPadding(), randomPadding()), new PointF(randomPadding(), image.Height - randomPadding()), new PointF(image.Width - randomPadding(), image.Height - randomPadding()), }; path.Warp(points, imageRect); graphic.FillPath( new HatchBrush(HatchStyle.LargeConfetti, Color.Black), path); } // 储存到会话,会话的过期时间最少是30分钟 var sessionManager = Application.Ioc.Resolve<SessionManager>(); var session = sessionManager.GetSession(); session.Items[SessionItemKeyPrefix + key] = captchaCode; session.SetExpiresAtLeast(TimeSpan.FromMinutes(MakeSessionAliveAtLeast)); sessionManager.SaveSession(); // 返回图片对象 return image; }
private void Form1_Paint(object sender, PaintEventArgs e) { GraphicsPath Path = new GraphicsPath(); Path.AddString("한글", new FontFamily("궁서"), 0, 100, new Point(10, 30), new StringFormat()); /* 확장 후 외곽선 그리기 Path.Widen(new Pen(Color.Black, 3)); e.Graphics.DrawPath(Pens.Black, Path); //*/ /* 확장 후 채우기 Path.Widen(new Pen(Color.Black, 3)); e.Graphics.FillPath(Brushes.Blue, Path); //*/ /* 곡선 펴기 Path.Flatten(new Matrix(), 12f); e.Graphics.DrawPath(Pens.Black, Path); //*/ /* 회전 Matrix M = new Matrix(); M.Rotate(-10); Path.Transform(M); e.Graphics.FillPath(Brushes.Blue, Path); //*/ //* 휘기 RectangleF R = Path.GetBounds(); PointF[] arPoint = new PointF[4]; arPoint[0] = new PointF(R.Left, R.Top + 30); arPoint[1] = new PointF(R.Right, R.Top - 10); arPoint[2] = new PointF(R.Left + 10, R.Bottom - 10); arPoint[3] = new PointF(R.Right + 30, R.Bottom + 30); Path.Warp(arPoint, R); e.Graphics.FillPath(Brushes.Blue, Path); //*/ }
public override GraphicsPath Create(GraphicsPath path, XCaptcha.ICanvas canvas) { var random = new Random(); var rect = new Rectangle(0, 0, canvas.Width, canvas.Height); const float wrapFactor = 8F; PointF[] points = { new PointF(random.Next(rect.Width) / wrapFactor,random.Next(rect.Height) / wrapFactor), new PointF(rect.Width - random.Next(rect.Width) / wrapFactor, random.Next(rect.Height) / wrapFactor), new PointF(random.Next(rect.Width) / wrapFactor, rect.Height - random.Next(rect.Height) / wrapFactor), new PointF(rect.Width - random.Next(rect.Width) / wrapFactor, rect.Height - random.Next(rect.Height) / wrapFactor) }; var matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); return path; }
public void GenerateImage() { //---- WITHOUT an image for the background ------ // Create a new 32-bit bitmap image. Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, this.width, this.height); // Fill in the background. //HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, ColorTranslator.FromHtml("#607f20"), ColorTranslator.FromHtml("#ffea00")); //HatchBrush hatchBrush = new HatchBrush(HatchStyle.Trellis, ColorTranslator.FromHtml("#ffffff"), ColorTranslator.FromHtml("#607f20")); HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.FromArgb(114,172,236), Color.FromArgb(161,214,255)); //Color.FromArgb(76,136,198) medium //Color.FromArgb(0,79,136) dark //Color.FromArgb(114,172,236) medium-light //Color.FromArgb(135,188,254) light //Color.FromArgb(161,214,255) really light g.FillRectangle(hatchBrush, rect); //---- WITH a background image ---------- //string bgpath = HttpContext.Current.Server.MapPath("CaptchaBG.bmp"); //Bitmap bitmap = new Bitmap(bgpath); //Graphics g = Graphics.FromImage(bitmap); //g.SmoothingMode = SmoothingMode.AntiAlias; //HatchBrush hatchBrush = null; //Rectangle rect = new Rectangle(0, 0, this.width, this.height); //----------------------------------------- // Set up the text font. SizeF size; float fontSize = this.height + 4; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font(this.familyName, fontSize, FontStyle.Bold); size = g.MeasureString(this.text, font); } while (size.Width > this.width); // Set up the text format. StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format); float v = 4F; PointF[] points = { new PointF(random.Next(this.width) / v, random.Next(this.height) / v), new PointF(this.width - random.Next(this.width) / v, random.Next(this.height) / v), new PointF(random.Next(this.width) / v, this.height - random.Next(this.height) / v), new PointF(this.width - random.Next(this.width) / v, this.height - random.Next(this.height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); // Draw the text. hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, ColorTranslator.FromHtml("#000000"), ColorTranslator.FromHtml("#000000")); // white numbers // hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, ColorTranslator.FromHtml("#ffffff"), ColorTranslator.FromHtml("#ffffff")); // yellow numbers //hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, ColorTranslator.FromHtml("#ffea00"), ColorTranslator.FromHtml("#ffea00")); g.FillPath(hatchBrush, path); //// Add some random noise. int m = Math.Max(this.width, this.height); for (int i = 0; i < (int)(this.width * this.height / 30F); i++) { int x = random.Next(this.width); int y = random.Next(this.height); int w = random.Next(m / 50); int h = random.Next(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } // Clean up. font.Dispose(); hatchBrush.Dispose(); g.Dispose(); // Set the image. this.image = bitmap; }
private void GenerateImage() { Bitmap bitmap = new Bitmap (this.width, this.height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, this.width, this.height); HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White); g.FillRectangle(hatchBrush, rect); SizeF size; float fontSize = rect.Height + 1; Font font; do { fontSize--; font = new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Bold); size = g.MeasureString(this.text, font); } while (size.Width > rect.Width); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; GraphicsPath path = new GraphicsPath(); //path.AddString(this.text, font.FontFamily, (int) font.Style, // font.Size, rect, format); path.AddString(this.text, font.FontFamily, (int)font.Style, 75, rect, format); float v = 4F; PointF[] points = { new PointF(this.random.Next(rect.Width) / v, this.random.Next( rect.Height) / v), new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v), new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v), new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); hatchBrush = new HatchBrush(HatchStyle.Percent10, Color.Black, Color.SkyBlue); g.FillPath(hatchBrush, path); int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = this.random.Next(rect.Width); int y = this.random.Next(rect.Height); int w = this.random.Next(m / 50); int h = this.random.Next(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } font.Dispose(); hatchBrush.Dispose(); g.Dispose(); this.image = bitmap; }
/// ----------------------------------------------------------------------------- /// <summary> /// Warps the Text /// </summary> /// <param name="textPath">The Graphics Path for the text</param> /// <param name="rect">a rectangle which defines the image</param> /// <history> /// [cnurse] 03/17/2006 created /// </history> /// ----------------------------------------------------------------------------- private static void WarpText(ref GraphicsPath textPath, Rectangle rect) { int intWarpDivisor; var rectF = new RectangleF(0, 0, rect.Width, rect.Height); intWarpDivisor = _Rand.Next(4, 8); int intHrange = Convert.ToInt32(rect.Height/intWarpDivisor); int intWrange = Convert.ToInt32(rect.Width/intWarpDivisor); PointF p1 = RandomPoint(0, intWrange, 0, intHrange); PointF p2 = RandomPoint(rect.Width - (intWrange - Convert.ToInt32(p1.X)), rect.Width, 0, intHrange); PointF p3 = RandomPoint(0, intWrange, rect.Height - (intHrange - Convert.ToInt32(p1.Y)), rect.Height); PointF p4 = RandomPoint(rect.Width - (intWrange - Convert.ToInt32(p3.X)), rect.Width, rect.Height - (intHrange - Convert.ToInt32(p2.Y)), rect.Height); var points = new[] {p1, p2, p3, p4}; var m = new Matrix(); m.Translate(0, 0); textPath.Warp(points, rectF, m, WarpMode.Perspective, 0); }
/// <summary> /// The generate image. /// </summary> private void GenerateImage() { var r = new Random(); // Create a new 32-bit bitmap image. var bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; var rect = new Rectangle(0, 0, this.width, this.height); var randomLineColor = this.random.Next(40) + 200; // Fill in the background. var hatchBrush = new HatchBrush( HatchStyle.SmallConfetti, Color.FromArgb(randomLineColor, randomLineColor, randomLineColor), Color.White); g.FillRectangle(hatchBrush, rect); // Set up the text font. SizeF size; float fontSize = rect.Height + 1; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font(this.familyName, fontSize, FontStyle.Bold); size = g.MeasureString(this.text, font); } while (size.Width > rect.Width); // Set up the text format. var format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // Create a path using the text and warp it randomly. var path = new GraphicsPath(); path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format); float v = 4F; PointF[] points = { new PointF(r.Next(rect.Width) / v, r.Next(rect.Height) / v), new PointF(rect.Width - r.Next(rect.Width) / v, r.Next(rect.Height) / v), new PointF(r.Next(rect.Width) / v, rect.Height - r.Next(rect.Height) / v), new PointF(rect.Width - r.Next(rect.Width) / v, rect.Height - r.Next(rect.Height) / v) }; var matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); var randomColor = Color.FromArgb( this.random.Next(100) + 100, this.random.Next(100) + 100, this.random.Next(100) + 100); var randomBackground = Color.FromArgb( 20 + this.random.Next(100), 20 + this.random.Next(100), 20 + this.random.Next(100)); // Draw the text. hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, randomColor, randomBackground); g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = r.Next(rect.Width); int y = r.Next(rect.Height); int w = r.Next(m / (this.random.Next(1000) + 50)); int h = r.Next(m / (this.random.Next(1000) + 50)); g.FillEllipse(hatchBrush, x, y, w, h); } double noise = this.random.Next(35) + 35; int maxDim = Math.Max(rect.Width, rect.Height); var radius = (int)(maxDim * noise / 3000); var maxGran = (int)(rect.Width * rect.Height / (100 - (noise >= 90 ? 90 : noise))); for (int i = 0; i < maxGran; i++) { g.FillEllipse( hatchBrush, this.random.Next(rect.Width), this.random.Next(rect.Height), this.random.Next(radius), this.random.Next(radius)); } double _lines = this.random.Next(25) + 15; if (_lines > 0) { int lines = ((int)_lines / 30) + 1; using (var pen = new Pen(hatchBrush, 1)) for (int i = 0; i < lines; i++) { var pointsLine = new PointF[lines > 2 ? lines - 1 : 2]; for (int j = 0; j < pointsLine.Length; j++) { pointsLine[j] = new PointF(this.random.Next(rect.Width), this.random.Next(rect.Height)); } g.DrawCurve(pen, pointsLine, 1.75F); } } // Clean up. font.Dispose(); hatchBrush.Dispose(); g.Dispose(); // Set the image. this.image = bitmap; }
private static void GenerateImage() { int width = 210, height = 70; string familyName = "Arial"; string text = ""; string temp = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; for (int i = 0; i < 4; ++i) { text += temp[rand.Next(temp.Length)]; } // Create a new 32-bit bitmap image. Bitmap bitmap = new Bitmap( width, height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, width, height); // Fill in the background. HatchBrush hatchBrush = new HatchBrush( HatchStyle.Shingle, //Color.LightGray, //Color.White); Color.FromArgb(rand.Next(224, 256), rand.Next(224, 256), rand.Next(224, 256)), Color.FromArgb(rand.Next(192, 224), rand.Next(192, 224), rand.Next(192, 224))); g.FillRectangle(hatchBrush, rect); // Set up the text font. float fontSize = rect.Height; Font font = new Font(familyName, fontSize, FontStyle.Bold); SizeF size = g.MeasureString(text, font); // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font( familyName, fontSize, FontStyle.Bold); size = g.MeasureString(text, font); } while (size.Width > rect.Width); // Set up the text format. StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(text, font.FontFamily, (int)font.Style, font.Size, rect, format); float v = 10; PointF[] points ={ new PointF(rand.Next(rect.Width)/v-20,rand.Next(rect.Height)/v), new PointF(rect.Width - rand.Next(rect.Width)/v-20,rand.Next(rect.Height)/v), new PointF(rand.Next(rect.Width)/v-20,rect.Height - rand.Next(rect.Height)/v), new PointF(rect.Width - rand.Next(rect.Width)/v-20,rect.Height - rand.Next(rect.Height)/v) }; for (int i = 0; i < 1; ++i) { System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); } // Draw the text. hatchBrush = new HatchBrush( HatchStyle.LargeConfetti, //Color.DarkGray, //Color.Black); Color.FromArgb(rand.Next(64, 128), rand.Next(64, 128), rand.Next(64, 128)), Color.FromArgb(rand.Next(0, 64), rand.Next(0, 64), rand.Next(0, 64))); g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = rand.Next(rect.Width); int y = rand.Next(rect.Height); int w = rand.Next(m / 50); int h = rand.Next(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } // Clean up. font.Dispose(); hatchBrush.Dispose(); g.Dispose(); for (int i = 0; i < 1; ++i) bitmap = wave(bitmap); // Set the image. bitmap.Save(Path.Combine(@"D:\Play Data\my_captcha", text + ".jpg"), ImageFormat.Jpeg); }
public void Wrap_SinglePoint () { using (GraphicsPath gp = new GraphicsPath ()) { gp.AddLines (new Point[1] { new Point (1, 1) }); // Special case - a line with a single point is valid Assert.AreEqual (1, gp.PointCount, "PointCount-1"); PointF[] pts = new PointF[1] { new PointF (0, 0) }; RectangleF r = new RectangleF (10, 20, 30, 40); gp.Warp (pts, r, new Matrix ()); Assert.AreEqual (0, gp.PointCount, "PointCount-2"); } }
private void generateImage() { if (!isDirty) return; // Create a new 32-bit bitmap image. Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. using (Graphics g = Graphics.FromImage(bitmap)) { g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, width, height); // Fill in the background. HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, backgroundDark, backgroundLight); g.FillRectangle(hatchBrush, rect); // Set up the text font. SizeF size; float fontSize = rect.Height + 1; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font(familyName, fontSize, fontStyle); size = g.MeasureString(text, font); } while (size.Width > rect.Width); // Set up the text format. StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(text, font.FontFamily, (int) font.Style, font.Size, rect, format); Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); if (System.Environment.OSVersion.Platform != PlatformID.Unix) { float v = 4F; PointF[] points = { new PointF(random.Next(rect.Width)/v, random.Next(rect.Height)/v), new PointF(rect.Width - random.Next(rect.Width)/v, random.Next(rect.Height)/v), new PointF(random.Next(rect.Width)/v, rect.Height - random.Next(rect.Height)/v), new PointF(rect.Width - random.Next(rect.Width)/v, rect.Height - random.Next(rect.Height)/v) }; path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); } else { float xtr = (rect.Size.Width - size.Width)/2F+size.Width/10F; float ytr = (rect.Size.Height - size.Height)/2F; matrix.Rotate((float)random.NextDouble()*10F-5F); matrix.Shear((float)random.NextDouble()*1F-0.5F,0F); matrix.Translate(xtr,ytr); path.Transform(matrix); } // Draw the text. using (hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, foregroundLight, foregroundDark)) { g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int) (rect.Width*rect.Height/30F); i++) { int x = random.Next(rect.Width); int y = random.Next(rect.Height); int w = random.Next(m/50); int h = random.Next(m/50); g.FillEllipse(hatchBrush, x, y, w, h); } } // Clean up. font.Dispose(); } // Set the image. image = bitmap; isDirty = false; }
/// <summary> /// Applies the specified image transformation. /// </summary> /// <param name="image">The image to be transfored.</param> /// <param name="parameters">The transformation params.</param> public void Apply(ref Image image, NameValueCollection parameters) { DynamicText text; if (!TextImageTransformation.ParseTextParam(parameters, out text)) { throw new ArgumentNullException("text"); } CaptchaStyle distStyle; if (!CaptchaImageTransformation.ParseDistorsionParam(parameters, out distStyle)) { distStyle = CaptchaStyle.Confetti; } ReadnessLevel readnessLevel; if (!CaptchaImageTransformation.ParseReadnessParam(parameters, out readnessLevel)) { readnessLevel = ReadnessLevel.Normal; } Color backColor; if (!CaptchaImageTransformation.ParseBackColorParam(parameters, out backColor)) { backColor = Color.White; } switch (distStyle) { case CaptchaStyle.Confetti: { using (Graphics g = Graphics.FromImage(image)) { Color middleColor = Color.FromArgb( text.Color.R + (backColor.R - text.Color.R) / 2, text.Color.G + (backColor.G - text.Color.G) / 2, text.Color.B + (backColor.B - text.Color.B) / 2); Rectangle rectangle = new Rectangle(Point.Empty, image.Size); // fill in the background using (Brush brush = new HatchBrush(HatchStyle.SmallConfetti, middleColor, backColor)) { g.FillRectangle(brush, rectangle); } Random rand = new Random(DateTime.Now.Millisecond); using (GraphicsPath path = new GraphicsPath()) { using (StringFormat format = new StringFormat()) { format.LineAlignment = text.HorizontalAlign; format.Alignment = text.VerticalAlign; path.AddString(text.Value, text.Font.FontFamily, (int)text.Font.Style, text.Font.Size, rectangle, format); } float mix = 4; PointF[] points = { new PointF(rand.Next(rectangle.Width) / mix, rand.Next(rectangle.Height) / mix), new PointF(rectangle.Width - rand.Next(rectangle.Width) / mix, rand.Next(rectangle.Height) / mix), new PointF(rand.Next(rectangle.Width) / mix, rectangle.Height - rand.Next(rectangle.Height) / mix), new PointF(rectangle.Width - rand.Next(rectangle.Width) / mix, rectangle.Height - rand.Next(rectangle.Height) / mix) }; path.Warp(points, rectangle, new Matrix(), WarpMode.Perspective, 0f); // draw the text using (HatchBrush brush = new HatchBrush(HatchStyle.LargeConfetti, middleColor, text.Color)) { g.FillPath(brush, path); } } // add some random noise int level; switch (readnessLevel) { case ReadnessLevel.Normal: { level = 50; break; } case ReadnessLevel.Hard: { level = 30; break; } case ReadnessLevel.AlmostImpossible: { level = 20; break; } default: { throw new NotImplementedException(); } } int m = Math.Max(rectangle.Width, rectangle.Height); for (int i = 0; i < (int)(rectangle.Width * rectangle.Height / 30); i++) { using (HatchBrush brush = new HatchBrush(HatchStyle.LargeConfetti, middleColor, text.Color)) { g.FillEllipse(brush, rand.Next(rectangle.Width), rand.Next(rectangle.Height), rand.Next(m / level), rand.Next(m / level)); } } } break; } case CaptchaStyle.Gradient: { using (Graphics g = Graphics.FromImage(image)) { g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rectangle = new Rectangle(Point.Empty, image.Size); Random rand = new Random(DateTime.Now.Millisecond); // draw the background as a random linear gradient using (LinearGradientBrush brush = new LinearGradientBrush( rectangle, Color.FromArgb(Color.White.R - backColor.R, Color.White.G - backColor.G, Color.White.B - backColor.B), backColor, (float)rand.NextDouble() * 360, false)) { g.FillRectangle(brush, rectangle); } // draw the string into a clone image, distort the text and // copy into the original image Size textSize = g.MeasureString(text.Value, text.Font).ToSize(); Size maxDistortion = new Size((rectangle.Width - textSize.Width) / 2 - 1, (rectangle.Height - textSize.Height) / 2 - 1); Size distortion; if (!Size.Empty.Equals(maxDistortion)) { switch (readnessLevel) { case ReadnessLevel.Normal: { distortion = new Size((maxDistortion.Width / 3) * ((rand.Next(2) == 1) ? 1 : -1), (maxDistortion.Height / 3) * ((rand.Next(2) == 1) ? 1 : -1)); break; } case ReadnessLevel.Hard: { distortion = new Size((maxDistortion.Width / 2) * ((rand.Next(2) == 1) ? 1 : -1), (maxDistortion.Height / 2) * ((rand.Next(2) == 1) ? 1 : -1)); break; } case ReadnessLevel.AlmostImpossible: { distortion = new Size(maxDistortion.Width * ((rand.Next(2) == 1) ? 1 : -1), maxDistortion.Height * ((rand.Next(2) == 1) ? 1 : -1)); break; } default: { throw new NotImplementedException(); } } using (GraphicsPath textPath = new GraphicsPath()) using (StringFormat format = new StringFormat()) { format.LineAlignment = text.HorizontalAlign; format.Alignment = text.VerticalAlign; textPath.AddString(text.Value, text.Font.FontFamily, (int)text.Font.Style, text.Font.Size, rectangle, format); PointF[] points = textPath.PathPoints; PointF[] distortedPoints = new PointF[textPath.PointCount]; for (int i = 0; i < textPath.PointCount; i++) { distortedPoints[i] = new PointF( points[i].X, //(float)(textPath.PathPoints[i].X + (distortion.Width * Math.Sin(Math.PI * textPath.PathPoints[i].Y / 48.0))), (float)(points[i].Y + (distortion.Height * Math.Cos(Math.PI * points[i].X / 48.0)))); } Color endColor = Color.FromArgb(Color.White.R - text.Color.R, Color.White.G - text.Color.G, Color.White.B - text.Color.B); using (GraphicsPath distortedTextPath = new GraphicsPath(distortedPoints, textPath.PathTypes)) using (LinearGradientBrush brush = new LinearGradientBrush( new Rectangle(0, 0, rectangle.Width / 2, rectangle.Height / 2), text.Color, endColor, (float)rand.NextDouble() * 360, false)) { g.FillPath(brush, distortedTextPath); } } } } break; } case CaptchaStyle.Holes: { using (Graphics g = Graphics.FromImage(image)) { g.Clear(backColor); Random rand = new Random(DateTime.Now.Millisecond); int noCircles = 0; switch (readnessLevel) { case ReadnessLevel.Normal: { noCircles = 1 * text.Value.Length + rand.Next(text.Value.Length); break; } case ReadnessLevel.Hard: { noCircles = 2 * text.Value.Length + rand.Next(text.Value.Length); break; } case ReadnessLevel.AlmostImpossible: { noCircles = 4 * text.Value.Length + rand.Next(text.Value.Length); break; } default: { throw new NotImplementedException(); } } Rectangle rectangle = new Rectangle(Point.Empty, image.Size); using (GraphicsPath textPath = new GraphicsPath()) { using (Brush brush = new SolidBrush(text.Color)) { using (StringFormat format = new StringFormat()) { format.LineAlignment = text.HorizontalAlign; format.Alignment = text.VerticalAlign; textPath.AddString(text.Value, text.Font.FontFamily, (int)text.Font.Style, text.Font.Size, rectangle, format); } int mix = 6; PointF[] points = { new PointF(rand.Next(rectangle.Width) / mix, rand.Next(rectangle.Height) / mix), new PointF(rectangle.Width - rand.Next(rectangle.Width) / mix, rand.Next(rectangle.Height) / mix), new PointF(rand.Next(rectangle.Width) / mix, rectangle.Height - rand.Next(rectangle.Height) / mix), new PointF(rectangle.Width - rand.Next(rectangle.Width) / mix, rectangle.Height - rand.Next(rectangle.Height) / mix) }; textPath.Warp(points, rectangle, new Matrix(), WarpMode.Perspective, 0); g.FillPath(brush, textPath); } float circleHalfWidth = text.Font.Size / 10; using (GraphicsPath circlePath = new GraphicsPath()) { for (int i = 0; i < noCircles; i++) { PointF textPathPoint = textPath.PathPoints[rand.Next(textPath.PathPoints.Length)]; circlePath.AddEllipse(new RectangleF(new PointF(textPathPoint.X - circleHalfWidth, textPathPoint.Y - circleHalfWidth), new SizeF(circleHalfWidth * 2, circleHalfWidth * 2))); } using (Brush brush = new SolidBrush(backColor)) { g.FillPath(brush, circlePath); } } } } break; } default: { throw new NotSupportedException(); } } }
/*public static void generateCaptcha(HttpContext context, int length, randomType type, int width, int height, System.Drawing.Color top_color, System.Drawing.Color bottom_color, System.Drawing.Color text_color) * { * System.Drawing.Drawing2D.HatchBrush hatchBrush = null; * * string embedded_string = commonFunctions.generateRandom(length, type); * * commonVariables.SetSessionVariable("vCode", commonEncryption.encrypting(embedded_string)); * * char[] char_array = null; * * char_array = new char[embedded_string.Length + embedded_string.Length]; * * for (int int_count = 0; int_count < embedded_string.Length; int_count++) * { * char_array[int_count + int_count] = embedded_string[int_count]; * char_array[int_count + int_count + 1] = " "[0]; * } * * //Captcha String * System.Drawing.FontFamily fontFamily = new System.Drawing.FontFamily("Comic Sans MS"); * // - Generate Random * //int randomsize = 5; * Random random = new Random(DateTime.Now.Millisecond); * * // Create a new 32-bit bitmap image. * using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) * { * // Create a graphics object for drawing. * using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) * { * g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; * System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height); * * // Fill in the background. * using (hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.DiagonalCross, top_color, bottom_color)) * { * g.FillRectangle(hatchBrush, rect); * * // Set up the text font. * System.Drawing.SizeF size = default(System.Drawing.SizeF); * float fontSize = rect.Height + 25; * System.Drawing.Font font = null; * System.Drawing.StringFormat format = new System.Drawing.StringFormat(); * format.Alignment = System.Drawing.StringAlignment.Center; * format.LineAlignment = System.Drawing.StringAlignment.Center; * * // Adjust the font size until the text fits within the image. * do * { * fontSize -= 5; * font = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold); * size = g.MeasureString(new string(char_array), font, new System.Drawing.SizeF(width, height), format); * } while (size.Width > rect.Width); * * // Create a path using the text and warp it randomly. * System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); * path.AddString(new string(char_array), font.FontFamily, Convert.ToInt32(font.Style), font.Size, rect, format); * * float v = 6f; * System.Drawing.PointF[] points = { * new System.Drawing.PointF(random.Next(rect.Width) / v, random.Next(rect.Height) / v), * new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, random.Next(rect.Height) / v), * new System.Drawing.PointF(random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v), * new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v) * }; * System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix(); * matrix.Translate(0f, 0f); * path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0f); * * // Draw the text. * using (hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, text_color, text_color)) * { * g.FillPath(hatchBrush, path); * } * * font.Dispose(); * } * } * * context.Response.ContentType = "image/png"; * bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); * } * }*/ public static string generateCaptcha(HttpContext context, int length, randomType type, int width, int height, System.Drawing.Color top_color, System.Drawing.Color bottom_color, System.Drawing.Color text_color) { System.Drawing.Drawing2D.HatchBrush hatchBrush = null; string strCode = commonFunctions.generateRandom(length, type); commonVariables.SetSessionVariable("vCode", commonEncryption.encrypting(strCode)); string strProcessRemark = "CommonFunction:" + strCode; int intProcessSerialId = 0; intProcessSerialId += 1; commonAuditTrail.appendLog("system", "CommonFunction", "ParameterValidation", "DataBaseManager.DLL", "", "", "", "", strProcessRemark, Convert.ToString(intProcessSerialId), "", true); //context.Session["vCode"] = commonEncryption.encrypting(strCode); char[] char_array = null; char_array = new char[strCode.Length + strCode.Length]; for (int int_count = 0; int_count < strCode.Length; int_count++) { char_array[int_count + int_count] = strCode[int_count]; char_array[int_count + int_count + 1] = " "[0]; } //Captcha String System.Drawing.FontFamily fontFamily = new System.Drawing.FontFamily("Comic Sans MS"); // - Generate Random //int randomsize = 5; Random random = new Random(DateTime.Now.Millisecond); // Create a new 32-bit bitmap image. using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { // Create a graphics object for drawing. using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, width, height); // Fill in the background. using (hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.DiagonalCross, top_color, bottom_color)) { g.FillRectangle(hatchBrush, rect); // Set up the text font. System.Drawing.SizeF size = default(System.Drawing.SizeF); float fontSize = rect.Height + 25; System.Drawing.Font font = null; System.Drawing.StringFormat format = new System.Drawing.StringFormat(); format.Alignment = System.Drawing.StringAlignment.Center; format.LineAlignment = System.Drawing.StringAlignment.Center; // Adjust the font size until the text fits within the image. do { fontSize -= 5; font = new System.Drawing.Font(fontFamily, fontSize, System.Drawing.FontStyle.Bold); size = g.MeasureString(new string(char_array), font, new System.Drawing.SizeF(width, height), format); } while (size.Width > rect.Width); // Create a path using the text and warp it randomly. System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); path.AddString(new string(char_array), font.FontFamily, Convert.ToInt32(font.Style), font.Size, rect, format); float v = 6f; System.Drawing.PointF[] points = { new System.Drawing.PointF(random.Next(rect.Width) / v, random.Next(rect.Height) / v), new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, random.Next(rect.Height) / v), new System.Drawing.PointF(random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v), new System.Drawing.PointF(rect.Width - random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v) }; System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix(); matrix.Translate(0f, 0f); path.Warp(points, rect, matrix, System.Drawing.Drawing2D.WarpMode.Perspective, 0f); // Draw the text. using (hatchBrush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, text_color, text_color)) { g.FillPath(hatchBrush, path); } font.Dispose(); } } context.Response.ContentType = "image/png"; bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); return(strCode); } }
// ==================================================================== // Creates the bitmap image. // ==================================================================== /// <summary> /// Generates the image. /// </summary> private void GenerateImage() { // Create a new 32-bit bitmap image. Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, Width, Height); // Fill in the background. HatchBrush hatchBrush = new HatchBrush(RandomHatchStyle, Color.WhiteSmoke, Color.LightGray); g.FillRectangle(hatchBrush, rect); // Set up the text font. SizeF size; float fontSize = rect.Height + 1; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font(FontFamily, fontSize, FontStyle.Regular); size = g.MeasureString(Text, font); } while (size.Width > rect.Width); // Set up the text format. StringFormat format = new StringFormat { Alignment = RandomStringAlignment, LineAlignment = RandomStringAlignment }; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(Text, font.FontFamily, (int)font.Style, font.Size, rect, format); const float v = 4F; PointF[] points = { new PointF(Randomizer.NextNumber(rect.Width) / v, Randomizer.NextNumber(rect.Height) / v), new PointF(rect.Width - Randomizer.NextNumber(rect.Width) / v, Randomizer.NextNumber(rect.Height) / v), new PointF(Randomizer.NextNumber(rect.Width) / v,rect.Height - Randomizer.NextNumber(rect.Height) / v), new PointF(rect.Width - Randomizer.NextNumber(rect.Width) / v,rect.Height - Randomizer.NextNumber(rect.Height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); // Draw the text. hatchBrush = new HatchBrush(RandomHatchStyle, Color.Gray, Color.LightGray); g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = Randomizer.NextNumber(rect.Width); int y = Randomizer.NextNumber(rect.Height); int w = Randomizer.NextNumber(m / 50); int h = Randomizer.NextNumber(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } Brush brush = new SolidBrush(Color.FromArgb(100, 68, 68, 68)); g.FillPath(brush, path); const double distort = 8d; // Copy the image so that we're always using the original for source color using (Bitmap copy = (Bitmap)bitmap.Clone()) { for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { // Adds a simple wave int newX = (int)(x + (distort * Math.Sin(Math.PI * y / 84.0))); int newY = (int)(y + (distort * Math.Cos(Math.PI * x / 44.0))); if (newX < 0 || newX >= Width) newX = 0; if (newY < 0 || newY >= Height) newY = 0; bitmap.SetPixel(x, y, copy.GetPixel(newX, newY)); } } } // Clean up. font.Dispose(); hatchBrush.Dispose(); g.Dispose(); // Set the image. Image = bitmap; }
public void Wrap_Line () { using (GraphicsPath gp = new GraphicsPath ()) { gp.AddLine (new Point (1, 1), new Point (20, 20)); Assert.AreEqual (2, gp.PointCount, "PointCount-1"); PointF[] pts = new PointF[1] { new PointF (0, 0) }; RectangleF r = new RectangleF (10, 20, 30, 40); gp.Warp (pts, r, new Matrix ()); Assert.AreEqual (2, gp.PointCount, "PointCount-2"); } }
protected override void Generate() { Random random = new Random(); int swidth = 12; int sheight = 20; int hspace = 3; int vspace = 3; _image = new Bitmap(Width, Height); using (Graphics graph = Graphics.FromImage(_image)) { graph.SmoothingMode = SmoothingMode.HighQuality; graph.TextRenderingHint = TextRenderingHint.AntiAlias; Rectangle rect = new Rectangle(0, 0, Width, Height); graph.Clear(BackColor); using (HatchBrush hatchBrush = new HatchBrush( HatchStyle.SmallConfetti, ForeColor, BackColor)) { graph.FillRectangle(hatchBrush, rect); StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Near; using (Font font = new Font(FontFamily, FontSize, FontStyle)) { for (int i = 0; i < Text.Length; i++) { int dh = random.Next(0, 8); Rectangle rect2 = new Rectangle(i*(swidth + 2*hspace), dh, (swidth + 2*hspace), sheight + dh); using (GraphicsPath path = new GraphicsPath()) { path.AddString(Text[i].ToString(), font.FontFamily, 1, font.Size, rect2, format); int h_offset1 = random.Next(-hspace, hspace); int v_offset1 = random.Next(-vspace, vspace); int h_offset2 = random.Next(-hspace, hspace); int v_offset2 = random.Next(-vspace, vspace); int h_offset3 = random.Next(-hspace, hspace); int v_offset3 = random.Next(-vspace, vspace); int h_offset4 = random.Next(-hspace, hspace); int v_offset4 = random.Next(-vspace, vspace); PointF[] points = { new PointF( i*(swidth + 2*hspace) + h_offset2, vspace + v_offset2), new PointF( (i + 1)*(swidth + 2*hspace) + h_offset4, vspace + v_offset4), new PointF( i*(swidth + 2*hspace) + h_offset1, sheight + v_offset1), new PointF( (i + 1)*(swidth + 2*hspace) + h_offset3, sheight + v_offset3) }; using (Matrix matrix = new Matrix()) { matrix.Translate(0F, 0F); path.Warp(points, rect2, matrix, WarpMode.Perspective, 1F); } using (HatchBrush hatchBrush2 = new HatchBrush( HatchStyle.Percent75, ForeColor, BackColor)) { graph.FillPath(hatchBrush2, path); } } } } } } }
public void Warp_EmptyMatrix () { PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); RectangleF r = new RectangleF (10, 20, 30, 40); path.Warp (pts, r, new Matrix ()); CheckWrap (path); }
private void WarpTextPath(GraphicsPath path, Rectangle rect) { if (FontWarp == FontWarpFactor.None) return; RectangleF warpRect = new RectangleF(rect.Left, 0, rect.Width, rect.Height); int hRange = (int) (rect.Height / WarpDivisor); int wRange = (int) (rect.Width / WarpDivisor); int left = (int) (rect.Left - (wRange * RangeModifier)); int top = (int) (rect.Top - (hRange * RangeModifier)); int width = (int) (rect.Left + rect.Width + (wRange * RangeModifier)); int height = (int) (rect.Top + rect.Height + (hRange * RangeModifier)); left = Math.Max(0, left); top = Math.Max(0, top); width = Math.Min(Width, width); height = Math.Min(Height, height); PointF leftTop = RandomPoint(left, left + wRange, top, top + hRange); PointF rightTop = RandomPoint(width - wRange, width, top, top + hRange); PointF leftBottom = RandomPoint(left, left + wRange, height - hRange, height); PointF rightBottom = RandomPoint(width - wRange, width, height - hRange, height); PointF[] points = new PointF[] {leftTop, rightTop, leftBottom, rightBottom}; Matrix matrix = new Matrix(); matrix.Translate(0, 0); path.Warp(points, warpRect, matrix, WarpMode.Perspective, 0); }
public void Warp_Rectangle_Empty () { PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); path.Warp (pts, new RectangleF (), null); CheckWrapNaN (path, true); }
public Bitmap Type2(string text, int width, int height) { //Create instance of bitmap object Bitmap objBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); //Create instance of graphics object Graphics objGraphics = Graphics.FromImage(objBitmap); objGraphics.SmoothingMode = SmoothingMode.AntiAlias; Rectangle objRect = new Rectangle(0, 0, width, height); //Fill the background in a light gray pattern HatchBrush objHatchBrush = new HatchBrush(HatchStyle.DiagonalCross, Color.LightGray, Color.White); objGraphics.FillRectangle(objHatchBrush, objRect); //Determine the appropriate font size SizeF objSize; float flFontSize = objRect.Height + 1; Font objFont; do //Decrease font size until text fits within the space { flFontSize--; objFont = new Font(System.Drawing.FontFamily.GenericSansSerif.Name, flFontSize, FontStyle.Bold); objSize = objGraphics.MeasureString(text, objFont); } while (objSize.Width > objRect.Width); //Format the text StringFormat objStringFormat = new StringFormat(); objStringFormat.Alignment = StringAlignment.Center; objStringFormat.LineAlignment = StringAlignment.Center; //Create a path using the text and randomly warp it GraphicsPath objGraphicsPath = new GraphicsPath(); objGraphicsPath.AddString(text, objFont.FontFamily, (int)objFont.Style, objFont.Size, objRect, objStringFormat); float flV = 4F; //Create a parallelogram for the text to draw into PointF[] arrPoints = { new PointF(HipRandom.Next(objRect.Width) / flV, HipRandom.Next(objRect.Height) / flV), new PointF(objRect.Width - HipRandom.Next(objRect.Width) / flV, HipRandom.Next(objRect.Height) / flV), new PointF(HipRandom.Next(objRect.Width) / flV, objRect.Height - HipRandom.Next(objRect.Height) / flV), new PointF(objRect.Width - HipRandom.Next(objRect.Width) / flV, objRect.Height - HipRandom.Next(objRect.Height) / flV) }; //Create the warped parallelogram for the text Matrix objMatrix = new Matrix(); objMatrix.Translate(0F, 0F); objGraphicsPath.Warp(arrPoints, objRect, objMatrix, WarpMode.Perspective, 0F); //Add the text to the shape objHatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.DarkGray, Color.Black); objGraphics.FillPath(objHatchBrush, objGraphicsPath); //Add some random noise int intMax = Math.Max(objRect.Width, objRect.Height); for (int i = 0; i < (int)(objRect.Width * objRect.Height / 30F); i++) { int x = HipRandom.Next(objRect.Width); int y = HipRandom.Next(objRect.Height); int w = HipRandom.Next(intMax / 15); int h = HipRandom.Next(intMax / 70); objGraphics.FillEllipse(objHatchBrush, x, y, w, h); } //Release memory objFont.Dispose(); objHatchBrush.Dispose(); objGraphics.Dispose(); //Set the public property to the return objBitmap; }
public void Warp_Rectangle_NegativeWidthHeight () { PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); RectangleF r = new RectangleF (10, 20, -30, -40); path.Warp (pts, r, null); Assert.AreEqual (3, path.PointCount, "Count"); pts = path.PathPoints; Assert.AreEqual (1.131355e-39, pts[0].X, 1e40, "0.X"); Assert.AreEqual (-2.0240637E-33, pts[0].Y, 1e40, "0.Y"); Assert.AreEqual (1.070131E-39, pts[1].X, 1e40, "1.X"); Assert.AreEqual (-2.02406389E-33, pts[1].Y, 1e40, "1.Y"); Assert.AreEqual (3.669146E-40, pts[2].X, 1e40, "2.X"); Assert.AreEqual (-6.746879E-34, pts[2].Y, 1e40, "2.Y"); byte[] types = path.PathTypes; Assert.AreEqual (0, types[0], "0"); Assert.AreEqual (1, types[1], "1"); Assert.AreEqual (129, types[2], "2"); }
public static void DrawValidateCode(Page page) { page.Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1)); page.Response.Cache.SetCacheability(HttpCacheability.NoCache); page.Response.AppendHeader("Pragma", "No-Cache"); page.Session["ValidateCode"] = MakeValidateCode(); // Set background color Rectangle rect = new Rectangle(0, 0, 100, 32); HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White); g.FillRectangle(hatchBrush, rect); // Set up the text font. SizeF size; float fontSize = rect.Height + 1; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font("Calibri", fontSize, FontStyle.Bold); size = g.MeasureString(page.Session["ValidateCode"].ToString(), font); } while (size.Width > rect.Width); // Set up the text format. StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(page.Session["ValidateCode"].ToString(), font.FontFamily, (int)font.Style, font.Size, rect, format); float v = 4F; Random r = new Random(); PointF[] points = { new PointF(r.Next(rect.Width) / v, r.Next(rect.Height) / v), new PointF(rect.Width - r.Next(rect.Width) / v, r.Next(rect.Height) / v), new PointF(r.Next(rect.Width) / v, rect.Height - r.Next(rect.Height) / v), new PointF(rect.Width - r.Next(rect.Width) / v, rect.Height - r.Next(rect.Height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); // Draw the text. hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray); g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = r.Next(rect.Width); int y = r.Next(rect.Height); int w = r.Next(m / 50); int h = r.Next(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } //g.DrawString(page.Session["ValidateCode"].ToString(), new Font("Calibri", 10, FontStyle.Regular), new SolidBrush(Color.FromArgb(153, 153, 255)), new PointF(0, 0)); g.Save(); using (MemoryStream ms = new MemoryStream()) { validateimage.Save(ms, ImageFormat.Png); page.Response.ClearContent(); page.Response.ContentType = "image/png"; page.Response.BinaryWrite(ms.ToArray()); } }
public void Warp_Matrix_NonInvertible () { Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30); Assert.IsFalse (matrix.IsInvertible, "!IsInvertible"); PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); RectangleF r = new RectangleF (10, 20, 30, 40); path.Warp (pts, r, matrix); Assert.AreEqual (3, path.PointCount, "Count"); pts = path.PathPoints; Assert.AreEqual (47, pts[0].X, "0.X"); Assert.AreEqual (30, pts[0].Y, "0.Y"); Assert.AreEqual (47, pts[1].X, "1.X"); Assert.AreEqual (30, pts[1].Y, "1.Y"); Assert.AreEqual (47, pts[2].X, "2.X"); Assert.AreEqual (30, pts[2].Y, "2.Y"); byte[] types = path.PathTypes; Assert.AreEqual (0, types[0], "0"); Assert.AreEqual (1, types[1], "1"); Assert.AreEqual (129, types[2], "2"); }
/// <summary> /// Warp the provided text GraphicsPath by a variable amount /// </summary> /// <param name="textPath">The text path.</param> /// <param name="rect">The rect.</param> private void WarpText(GraphicsPath textPath, Rectangle rect) { float WarpDivisor; float RangeModifier; switch (FontWarp) { case FontWarpFactor.None: goto default; case FontWarpFactor.Low: WarpDivisor = 6F; RangeModifier = 1F; break; case FontWarpFactor.Medium: WarpDivisor = 5F; RangeModifier = 1.3F; break; case FontWarpFactor.High: WarpDivisor = 4.5F; RangeModifier = 1.4F; break; case FontWarpFactor.Extreme: WarpDivisor = 4F; RangeModifier = 1.5F; break; default: return; } RectangleF rectF; rectF = new RectangleF(Convert.ToSingle(rect.Left), 0, Convert.ToSingle(rect.Width), rect.Height); int hrange = Convert.ToInt32(rect.Height / WarpDivisor); int wrange = Convert.ToInt32(rect.Width / WarpDivisor); int left = rect.Left - Convert.ToInt32(wrange * RangeModifier); int top = rect.Top - Convert.ToInt32(hrange * RangeModifier); int width = rect.Left + rect.Width + Convert.ToInt32(wrange * RangeModifier); int height = rect.Top + rect.Height + Convert.ToInt32(hrange * RangeModifier); if (left < 0) left = 0; if (top < 0) top = 0; if (width > this.Width) width = this.Width; if (height > this.Height) height = this.Height; PointF leftTop = RandomPoint(left, left + wrange, top, top + hrange); PointF rightTop = RandomPoint(width - wrange, width, top, top + hrange); PointF leftBottom = RandomPoint(left, left + wrange, height - hrange, height); PointF rightBottom = RandomPoint(width - wrange, width, height - hrange, height); PointF[] points = new PointF[] { leftTop, rightTop, leftBottom, rightBottom }; Matrix m = new Matrix(); m.Translate(0, 0); textPath.Warp(points, rectF, m, WarpMode.Perspective, 0); }
public void Warp_Bilinear () { PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); RectangleF r = new RectangleF (10, 20, 30, 40); path.Warp (pts, r, new Matrix (), WarpMode.Bilinear); // note that the last point is no more closed CheckWrapNaN (path, false); }
// ==================================================================== // Creates the bitmap image. // ==================================================================== private void GenerateImage() { // Create a new 32-bit bitmap image. Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, width, height); // Fill in the background. HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White); g.FillRectangle(hatchBrush, rect); // Set up the text font. SizeF size; float fontSize = rect.Height + 1; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font(familyName, fontSize, FontStyle.Bold); size = g.MeasureString(text, font); } while (size.Width > rect.Width); // Set up the text format. StringFormat format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(text, font.FontFamily, (int)font.Style, font.Size, rect, format); float v = 4F; PointF[] points = { new PointF(random.Next(rect.Width) / v, random.Next(rect.Height) / v), new PointF(rect.Width - random.Next(rect.Width) / v, random.Next(rect.Height) / v), new PointF(random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v), new PointF(rect.Width - random.Next(rect.Width) / v, rect.Height - random.Next(rect.Height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); // Draw the text. hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray); g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = random.Next(rect.Width); int y = random.Next(rect.Height); int w = random.Next(m / 50); int h = random.Next(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } // Clean up. font.Dispose(); hatchBrush.Dispose(); g.Dispose(); // Set the image. image = bitmap; }
public void Warp_Invalid () { PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); RectangleF r = new RectangleF (10, 20, 30, 40); path.Warp (pts, r, new Matrix (), (WarpMode) Int32.MinValue); Assert.AreEqual (0, path.PointCount, "Count"); }
public void GenerateImage() { // Create a new 32-bit bitmap image. Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb); // Create a graphics object for drawing. Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle rect = new Rectangle(0, 0, this.width, this.height); // Fill in the background. HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.FromArgb(114, 172, 236), Color.FromArgb(161, 214, 255)); //Color.FromArgb(76,136,198) medium //Color.FromArgb(0,79,136) dark //Color.FromArgb(114,172,236) medium-light //Color.FromArgb(135,188,254) light //Color.FromArgb(161,214,255) really light g.FillRectangle(hatchBrush, rect); // Set up the text font. SizeF size; float fontSize = rect.Height + 4; Font font; // Adjust the font size until the text fits within the image. do { fontSize--; font = new Font(this.familyName, fontSize, FontStyle.Bold); size = g.MeasureString(this.text, font); } while (size.Width > rect.Width); // Set up the text format. StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; // Create a path using the text and warp it randomly. GraphicsPath path = new GraphicsPath(); path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format); float v = 4F; PointF[] points = { new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v), new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v), new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v), new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v) }; Matrix matrix = new Matrix(); matrix.Translate(0F, 0F); path.Warp(points, rect, matrix, WarpMode.Perspective, 0F); // Draw the text. hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.FromArgb(0, 79, 136), Color.FromArgb(76, 136, 198)); g.FillPath(hatchBrush, path); // Add some random noise. int m = Math.Max(rect.Width, rect.Height); for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++) { int x = this.random.Next(rect.Width); int y = this.random.Next(rect.Height); int w = this.random.Next(m / 50); int h = this.random.Next(m / 50); g.FillEllipse(hatchBrush, x, y, w, h); } // Clean up. font.Dispose(); hatchBrush.Dispose(); g.Dispose(); // Set the image. this.image = bitmap; }
public void Warp_Flatness_OverOne () { PointF[] pts = new PointF[1] { new PointF (0, 0) }; GraphicsPath path = new GraphicsPath (); path.AddPolygon (new Point[3] { new Point (5, 5), new Point (15, 5), new Point (10, 15) }); RectangleF r = new RectangleF (10, 20, 30, 40); path.Warp (pts, r, new Matrix (), WarpMode.Perspective, 2.0f); CheckWrap (path); }