本文整理汇总了C#中System.Drawing.Drawing2D.GraphicsPath.AddString方法的典型用法代码示例。如果您正苦于以下问题:C# GraphicsPath.AddString方法的具体用法?C# GraphicsPath.AddString怎么用?C# GraphicsPath.AddString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Drawing.Drawing2D.GraphicsPath
的用法示例。
在下文中一共展示了GraphicsPath.AddString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetStringPath
static GraphicsPath GetStringPath(string s, float dpi, Point p, Font font, StringFormat format)
{
GraphicsPath path = new GraphicsPath();
// Convert font size into appropriate coordinates
float emSize = dpi * font.SizeInPoints / 72;
path.AddString(s, font.FontFamily, (int)font.Style, emSize, p, format);
return path;
}
示例2: DrawText
public static void DrawText(Font font, StringFormat sf, GraphicsPath path, StringBuilder sb, bool isItalic, bool isBold, bool isUnderline, float left, float top, ref bool newLine, float leftMargin, ref int pathPointsStart)
{
var next = new PointF(left, top);
if (path.PointCount > 0)
{
int k = 0;
var list = (PointF[])path.PathPoints.Clone(); // avoid using very slow path.PathPoints indexer!!!
for (int i = list.Length - 1; i >= 0; i--)
{
if (list[i].X > next.X)
next.X = list[i].X;
k++;
if (k > 60)
break;
if (i <= pathPointsStart && pathPointsStart != -1)
break;
}
}
if (newLine)
{
next.X = leftMargin;
newLine = false;
pathPointsStart = path.PointCount;
}
var fontStyle = FontStyle.Regular;
if (isItalic)
fontStyle |= FontStyle.Italic;
if (isBold)
fontStyle |= FontStyle.Bold;
if (isUnderline)
fontStyle |= FontStyle.Underline;
try
{
path.AddString(sb.ToString(), font.FontFamily, (int)fontStyle, font.Size, next, sf);
}
catch
{
fontStyle = FontStyle.Regular;
try
{
path.AddString(sb.ToString(), font.FontFamily, (int)fontStyle, font.Size, next, sf);
}
catch
{
path.AddString(sb.ToString(), new FontFamily("Arial"), (int)fontStyle, font.Size, next, sf);
}
}
sb.Length = 0;
}
示例3: Draw
public void Draw(KalikoImage image) {
var graphics = image.Graphics;
var graphicsPath = new GraphicsPath();
var stringFormat = new StringFormat {
Alignment = Alignment,
LineAlignment = VerticalAlignment
};
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
if (Font == null) {
Font = image.Font ?? new Font("Arial", 32, FontStyle.Bold, GraphicsUnit.Pixel);
}
if (TargetArea == Rectangle.Empty) {
TargetArea = new Rectangle(0, 0, image.Width, image.Height);
}
if (Point == Point.Empty) {
graphicsPath.AddString(Text, Font.FontFamily, (int)Font.Style, Font.Size, TargetArea, stringFormat);
}
else {
graphicsPath.AddString(Text, Font.FontFamily, (int)Font.Style, Font.Size, Point, stringFormat);
}
if (Rotation != 0) {
var rotationTransform = new Matrix(1, 0, 0, 1, 0, 0);
var bounds = graphicsPath.GetBounds();
rotationTransform.RotateAt(Rotation, new PointF(bounds.X + (bounds.Width / 2f), bounds.Y + (bounds.Height / 2f)));
graphicsPath.Transform(rotationTransform);
}
if (TextShadow != null) {
DrawShadow(graphics, graphicsPath);
}
if (Outline > 0) {
var pen = new Pen(OutlineColor, Outline) {
LineJoin = LineJoin.Round
};
graphics.DrawPath(pen, graphicsPath);
}
if (TextBrush == null) {
TextBrush = new SolidBrush(TextColor);
}
graphics.FillPath(TextBrush, graphicsPath);
}
示例4: BitmapFromText
public static Bitmap BitmapFromText(string text, Font f, Color col, Color glowCol, Rectangle rect, float glowScale)
{
Bitmap bm = new Bitmap((int)(rect.Width / glowScale), (int)(rect.Height / glowScale));
GraphicsPath pth = new GraphicsPath();
pth.AddString(text, f.FontFamily, (int)f.Style, f.Size, new Point(rect.Left, rect.Top), StringFormat.GenericTypographic);
Graphics g = Graphics.FromImage(bm);
Matrix mx = new Matrix(1.0f / glowScale, 0, 0, 1.0f / glowScale, -(1.0f / glowScale), -(1.0f / glowScale));
g.SmoothingMode = SmoothingMode.AntiAlias;
g.Transform = mx;
Pen p = new Pen(glowCol, 3);
g.DrawPath(p, pth);
g.FillPath(new SolidBrush(glowCol), pth);
g.Dispose();
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
Graphics g2 = Graphics.FromImage(bmp);
g2.Transform = new Matrix(1, 0, 0, 1, 50, 50);
g2.SmoothingMode = SmoothingMode.AntiAlias;
g2.InterpolationMode = InterpolationMode.HighQualityBicubic;
g2.DrawImage(bm, rect, 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel);
g2.FillPath(new SolidBrush(col), pth);
pth.Dispose();
return bmp;
}
示例5: drawOutlineText
/// <summary>
/// Draw an outlined text on a graphic.
/// Draw a text using an outline in it, from custom CardGraphicComponent values. These are used to get position of text and destination picture/graphic.
/// </summary>
/// <param name="g">Graphic to draw the text</param>
/// <param name="text">Text to draw</param>
/// <param name="canvas">Values for position, font, etc. of the text</param>
/// <param name="parent">If canvas has a parent, use it for relative position of the text</param>
/// <param name="centered">If text must be drawn centered or not</param>
public static void drawOutlineText(Graphics g, string text, CardGraphicComponent canvas, CardGraphicComponent parent, bool centered=false)
{
// Check parent to get proper offsets
var offsetTop = 0;
var offsetLeft = 0;
if (parent != null)
{
offsetLeft = parent.Left;
offsetTop = parent.Top;
}
// set atialiasing for drawing
g.SmoothingMode = SmoothingMode.HighQuality; // AntiAlias
g.InterpolationMode = InterpolationMode.HighQualityBicubic; // High
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Create GraphicsPath to draw text
using (GraphicsPath path = new GraphicsPath())
{
// Calculate position in case text must be draw centered or not
float txtWidth = 0;
if (centered)
txtWidth = getStringWidth(g, text, new Font(canvas.font.FontFamily, canvas.font.Size, canvas.font.Style, GraphicsUnit.Point, ((byte)(0)))) / 2;
// Add string to path
path.AddString(text, canvas.font.FontFamily, (int)canvas.font.Style, canvas.font.Size, new Point(canvas.Left + offsetLeft - (int)txtWidth, canvas.Top + offsetTop), StringFormat.GenericTypographic);
// Draw text using this pen. This does the trick (with the path) to draw the outline
using (Pen pen = new Pen(canvas.borderColor, canvas.Outline))
{
pen.LineJoin = LineJoin.Round;
g.DrawPath(pen, path);
g.FillPath(canvas.textColor, path);
}
}
}
示例6: Generate
/// <summary>
/// Creating an image for a Captcha.
/// </summary>
/// <param name="captchaText">Text Captcha.</param>
/// <returns></returns>
public Bitmap Generate(string captchaText)
{
var bmp = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bmp))
{
var rect = new Rectangle(0, 0, Width, Height);
graphics.SmoothingMode = SmoothingMode.HighQuality;
using (var solidBrush = new SolidBrush(Color.White))
{
graphics.FillRectangle(solidBrush, rect);
}
//Randomly choose the font name.
var family = _fonts[new Random().Next(0, _fonts.Length)];
var font = new Font(family, 30);
using (var fontFormat = new StringFormat())
{
//Format the font in the center.
fontFormat.Alignment = StringAlignment.Center;
fontFormat.LineAlignment = StringAlignment.Center;
var path = new GraphicsPath();
path.AddString(captchaText, font.FontFamily, (int)font.Style, font.Size, rect, fontFormat);
using (var solidBrush = new SolidBrush(Color.Blue))
{
graphics.FillPath(solidBrush, DeformPath(path));
}
}
font.Dispose();
}
return bmp;
}
示例7: DrawString
/// <summary>
/// Renders text along the path
/// </summary>
/// <param name="self">The graphics object</param>
/// <param name="halo">The pen to render the halo outline</param>
/// <param name="fill">The brush to fill the text</param>
/// <param name="text">The text to render</param>
/// <param name="fontFamily">The font family to use</param>
/// <param name="style">The style</param>
/// <param name="emSize">The size</param>
/// <param name="format">The format</param>
/// <param name="ignoreLength"></param>
/// <param name="path"></param>
public static void DrawString(this Graphics self, Pen halo, Brush fill, string text, FontFamily fontFamily, int style, float emSize, StringFormat format, bool ignoreLength, GraphicsPath path)
{
if (path == null || path.PointCount == 0)
return;
var gp = new GraphicsPath();
gp.AddString(text, fontFamily, style, emSize, new Point(0, 0), format);
SortedList<float, GraphSegment> edges;
var totalLength = GetPathLength(path, out edges);
var warpedPath = PrepareTextPathToWarp(gp, totalLength, ignoreLength, format);
if (warpedPath == null)
return;
var wp = Warp(path, warpedPath, false, 0f);
if (wp != null)
{
if (halo != null)
self.DrawPath(halo, wp);
if (fill != null)
self.FillPath(fill, wp);
}
}
示例8: CreateInfomationBitmap
private static Bitmap CreateInfomationBitmap(string content, int width, int height,
StringAlignment hAlign = StringAlignment.Near,
StringAlignment vAlign = StringAlignment.Near)
{
Bitmap b = new Bitmap(width, height);
Graphics g = Graphics.FromImage(b);
GraphicsPath path = new GraphicsPath();
FontFamily fontFamily = new FontFamily("微软雅黑");
StringFormat format = new StringFormat();
format.Alignment = hAlign;
format.LineAlignment = vAlign;
path.AddString(content,
fontFamily, (int)FontStyle.Bold, 16, new Rectangle(0, 0, width, height), format);
fontFamily.Dispose();
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillPath(Brushes.Black, path);
Pen pen = new Pen(Color.Black, 2);
g.DrawPath(pen, path);
g.Dispose();
pen.Dispose();
Bitmap infoBitmap = RenderUtils.BoxBlur(b, 1);
g = Graphics.FromImage(infoBitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillPath(Brushes.White, path);
g.Dispose();
return infoBitmap;
}
示例9: OnPaint
protected override void OnPaint(PaintEventArgs e) {
if (DesignMode || !Natives.CanUseAero) {
base.OnPaint(e);
return;
}
using (Bitmap b = new Bitmap(Width / 5, Height / 5))
using (GraphicsPath path = new GraphicsPath())
using (Graphics temp = Graphics.FromImage(b))
using (Pen glow = new Pen(Color.Red, 3))
using (Brush text = new SolidBrush(Color.Black))
using (Matrix matrix = new Matrix(1.0f / 5, 0, 0, 1.0f / 5, -(1.0f / 5), -(1.0f / 5))) {
path.AddString(Text, Font.FontFamily, (int)Font.Style, Font.Size, Location, StringFormat.GenericTypographic);
temp.SmoothingMode = SmoothingMode.AntiAlias;
temp.Transform = matrix;
temp.DrawPath(glow, path);
temp.FillPath(Brushes.Red, path);
e.Graphics.Transform = new Matrix(1, 0, 0, 1, 50, 50);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.DrawImage(b, ClientRectangle, 0, 0, b.Width, b.Height, GraphicsUnit.Pixel);
e.Graphics.FillPath(text, path);
}
}
示例10: DrawLabel
/// <summary>
/// Renders a label to the map.
/// </summary>
/// <param name="graphics">Graphics reference</param>
/// <param name="labelPoint">Label placement</param>
/// <param name="offset">Offset of label in screen coordinates</param>
/// <param name="font">Font used for rendering</param>
/// <param name="forecolor">Font forecolor</param>
/// <param name="backcolor">Background color</param>
/// <param name="halo">Color of halo</param>
/// <param name="rotation">Text rotation in degrees</param>
/// <param name="text">Text to render</param>
/// <param name="viewport"></param>
public static void DrawLabel(Graphics graphics, Point labelPoint, Offset offset, Styles.Font font, Styles.Color forecolor, Styles.Brush backcolor, Styles.Pen halo, double rotation, string text, IViewport viewport, StyleContext context)
{
SizeF fontSize = graphics.MeasureString(text, font.ToGdi(context)); //Calculate the size of the text
labelPoint.X += offset.X; labelPoint.Y += offset.Y; //add label offset
if (Math.Abs(rotation) > Constants.Epsilon && !double.IsNaN(rotation))
{
graphics.TranslateTransform((float)labelPoint.X, (float)labelPoint.Y);
graphics.RotateTransform((float)rotation);
graphics.TranslateTransform(-fontSize.Width / 2, -fontSize.Height / 2);
if (backcolor != null && backcolor.ToGdi(context) != Brushes.Transparent)
graphics.FillRectangle(backcolor.ToGdi(context), 0, 0, fontSize.Width * 0.74f + 1f, fontSize.Height * 0.74f);
var path = new GraphicsPath();
path.AddString(text, new FontFamily(font.FontFamily), (int)font.ToGdi(context).Style, font.ToGdi(context).Size, new System.Drawing.Point(0, 0), null);
if (halo != null)
graphics.DrawPath(halo.ToGdi(context), path);
graphics.FillPath(new SolidBrush(forecolor.ToGdi()), path);
//g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), 0, 0);
}
else
{
if (backcolor != null && backcolor.ToGdi(context) != Brushes.Transparent)
graphics.FillRectangle(backcolor.ToGdi(context), (float)labelPoint.X, (float)labelPoint.Y, fontSize.Width * 0.74f + 1, fontSize.Height * 0.74f);
var path = new GraphicsPath();
//Arial hack
path.AddString(text, new FontFamily("Arial"), (int)font.ToGdi(context).Style, (float)font.Size, new System.Drawing.Point((int)labelPoint.X, (int)labelPoint.Y), null);
if (halo != null)
graphics.DrawPath(halo.ToGdi(context), path);
graphics.FillPath(new SolidBrush(forecolor.ToGdi()), path);
//g.DrawString(text, font, new System.Drawing.SolidBrush(forecolor), LabelPoint.X, LabelPoint.Y);
}
}
示例11: OutlinedStringSurface
public OutlinedStringSurface(
String str, Font font, Brush brush, Pen outlinePen,
StringFormat stringFormat)
{
Contract.Requires(str != null);
Contract.Requires(font != null);
Contract.Requires(brush != null);
Contract.Requires(outlinePen != null);
_brush = brush;
_outlinePen = outlinePen;
// グラフィックスパスの生成
_path = new GraphicsPath();
_path.AddString(
str, font.FontFamily, (int)font.Style, font.Size,
new Point(0, 0), stringFormat);
// サイズを取得する
var rect = _path.GetBounds();
Size = rect.Size.ToSize();
// 描画時にマージンがなくなるように平行移動
var matrix = new Matrix(1, 0, 0, 1, -rect.Left, -rect.Top);
_path.Transform(matrix);
// 描画位置を(0, 0)で記憶
matrix.Reset();
_matrix = matrix;
}
示例12: OnPaint
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (BorderRadius == 0)
{
Rectangle rc = ClientRectangle;
rc.Inflate(-(int)Math.Round(BorderWidth / 2.0 + .5), -(int)Math.Round(BorderWidth / 2.0 + .5));
rc.Y = rc.Y - 1;
rc.Height = rc.Height + 1;
e.Graphics.DrawRectangle(new Pen(BorderColor, BorderWidth), rc);
}
else
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath gp = Extensions.Create(0, 0, Width - 2, Height - 2, BorderRadius);
Pen p = new Pen(BorderColor, BorderWidth);
e.Graphics.DrawPath(p, gp);
e.Graphics.FillPath(p.Brush, gp);
StringFormat formatting = (StringFormat)StringFormat.GenericTypographic.Clone();
formatting.Alignment = StringAlignment.Center;
formatting.LineAlignment = StringAlignment.Center;
float emsize = e.Graphics.DpiY * Font.Size / 72;
gp = new GraphicsPath();
gp.StartFigure();
gp.AddString(Text, Font.FontFamily, (int)Font.Style, emsize, new Point(Width / 2, Height / 2), formatting);
gp.CloseFigure();
e.Graphics.FillPath(new Pen(ForeColor, 1).Brush, gp);
}
}
示例13: GenerateImage
public Bitmap GenerateImage()
{
int a = random.Next(_maxNumber);
int b = random.Next(_maxNumber);
_answer = (a + b).ToString();
string text = string.Format("{0} + {1} = ", a, b);
Bitmap bitmap = new Bitmap(_width, _height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, _width, _height);
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, _foreColor, _bgColor);
g.FillRectangle(hatchBrush, rect);
SizeF size;
float fontSize = rect.Height + 1;
Font font;
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 = 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);
hatchBrush = new HatchBrush(HatchStyle.SolidDiamond, _foreColor, _foreColor);
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);
}
return bitmap;
}
示例14: Subtitle
public Subtitle(string content,
Font font, Color fillColor, Color borderColor, float borderWidth,
long startTime, long endTime)
{
StartTime = startTime;
EndTime = endTime;
IntPtr screenDc = ApiHelper.GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(screenDc);
SizeF gsize = g.MeasureString(content, font);
g.Dispose();
this.Size = Size.Ceiling(new SizeF(gsize.Width + borderWidth, gsize.Height + borderWidth));
hMemDc = ApiHelper.CreateCompatibleDC(screenDc);
hMemBitmap = ApiHelper.CreateCompatibleBitmap(screenDc, Size.Width, Size.Height);
ApiHelper.ReleaseDC(IntPtr.Zero, screenDc);
ApiHelper.SelectObject(hMemDc, hMemBitmap);
g = Graphics.FromHdc(hMemDc);
g.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath path = new GraphicsPath();
path.AddString(content, font.FontFamily, (int)font.Style, font.Size,
new Rectangle(Point.Empty, Size), defaultFormat);
Pen pen = new Pen(borderColor, borderWidth);
pen.LineJoin = LineJoin.Round;
pen.Alignment = PenAlignment.Outset;
g.DrawPath(pen, path);
pen.Dispose();
Brush brush = new SolidBrush(fillColor);
g.FillPath(brush, path);
brush.Dispose();
g.Dispose();
}
示例15: Form1
public Form1()
{
InitializeComponent();
Path = new GraphicsPath();
StringFormat sm = new StringFormat();
Path.AddString("한", new FontFamily("궁서"), 0, 200, new Point(10, 10), sm);
}