private void Draw(Graphics graphics, TextPath textPath, double scaleRatio, double angle, PointF point, Color color, double strokeWidth) { // 拡大率、傾き、位置に基づいて GraphicsPath を変換する。 textPath.Transform(scaleRatio, angle, point); // 色から、縁取りの色を求める。 var strokeColor = color.GetBrightness() > 0.90F ? Color.Black : Color.White; // 色、縁取りの色、縁取りの幅に基づいて Brush と Pen を作成する。 var brush = new SolidBrush(color); var pen = new Pen(strokeColor, (float)strokeWidth); graphics.DrawPath(pen, textPath.Path); graphics.FillPath(brush, textPath.Path); }
/// <summary> /// 指定した Graphics に、指定した文字列をランダムに描きます。 /// </summary> public void Scribble(string text, Graphics graphics, int width, int height) { // 画像のサイズから、最小フォントサイズを短辺の1/100と決定する。 var 短辺 = Math.Min(width, height); var minFontSize = 短辺 / 100F; // フォントファミリーとフォントスタイルをランダムに決定する。 var fontInfo = FontInfo.GenerateRandomFontInfo(text, random); // フォントファミリー、フォントスタイル、最小フォントサイズから、テキストパスサイズを求める。 var textPath = new TextPath(text, fontInfo, minFontSize); var pathBounds = textPath.Path.GetBounds(); // テキストパスサイズと画像サイズから、拡大率を決定する。 var scaleRatio = GenerateRandomScaleRatio(pathBounds.Width, pathBounds.Height, width, height); // 傾きを決定する。 var angle = GenerateRandomAngle(45); // 最小フォントサイズ、拡大率、テキストパスサイズ、画像サイズ、傾きから、位置を決定する。 var point = GenerateRandomPoint( scaleRatio, pathBounds.Width, pathBounds.Height, width, height, angle); // 色を決定する。色に基づいて Brush を作成する。 var color = GenerateRandomColor(); // フォントサイズから、縁取りの幅を決定する。 // フォントサイズ≒文字列高さなので、別に pathBounds.Height * scaleRatio を渡してもよい。 var strokeWidth = GenerateRandomStrokeWidth(minFontSize * scaleRatio); // 実際に描画。 Draw(graphics, textPath, scaleRatio, angle, point, color, strokeWidth); }