C#中实现文字按指定的路径排列
2012-08-28 13:18:15 来源:WEB开发网if (_count > 0)
{
_Point1 = _Points[_count];
}
else
{
_Point1 = _Points[0];
}
break;
case PathAlign.Right:
_count = (int)(_MaxPoints - _MaxWidthText - StringRegion(_G, _textTOP.Length - 1) * LetterSpacePercentage / 100);
if (_count > 0)
{
_Point1 = _Points[_count];
}
else
{
_Point1 = _Points[0];
}
break;
}
while (!(_CharStep > _textTOP.Length - 1))
{
lStrWidth = (int)StringRegion(_G, _CharStep) * LetterSpacePercentage / 100;
if ((_count + lStrWidth / 2) >= 0 & (_count + lStrWidth) < _MaxPoints)
{
_count += lStrWidth;
_Point2 = _Points[_count];
_Point = _Points[_count - lStrWidth / 2];
_Angle = GetAngle(_Point1, _Point2);
DrawRotatedText(_G, _textTOP[_CharStep].ToString(), (float)_Angle, _Point);
_Point1 = _Points[_count];
}
else
{
_count += lStrWidth;
}
_CharStep += 1;
}
_G.Dispose();
return _bitmap;
}
private float StringRegion(Graphics _g, int _textpos)
{
string measureString = _textTOP.Substring(_textpos, 1);
int numChars = measureString.Length;
CharacterRange[] characterRanges = new CharacterRange[numChars + 1];
StringFormat stringFormat = new StringFormat();
stringFormat.Trimming = StringTrimming.None;
stringFormat.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
SizeF size = _g.MeasureString(_textTOP, _fontTOP);
RectangleF layoutRect = new RectangleF(0f, 0f, size.Width, size.Height);
Region[] stringRegions = new Region[numChars + 1];
characterRanges[0] = new CharacterRange(0, 1);
stringFormat.FormatFlags = StringFormatFlags.NoClip;
stringFormat.SetMeasurableCharacterRanges(characterRanges);
stringFormat.Alignment = StringAlignment.Near;
stringRegions = _g.MeasureCharacterRanges(_textTOP.Substring(_textpos), _fontTOP, layoutRect, stringFormat);
return stringRegions[0].GetBounds(_g).Width;
}
private float[] MeasureWidths(Graphics graphics)
{
float[] widths = new float[_textTOP.Length + 1];
StringFormat format = StringFormat.GenericTypographic;
format.Trimming = StringTrimming.None;
format.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
RectangleF layout = new RectangleF(0, 0, 10000, 10000);
int remainder = _textTOP.Length;
int start = 0;
while (remainder > 0)
{
int length = remainder;
if (length > 32)
{
length = 32;
}
CharacterRange[] ranges = new CharacterRange[length + 1];
int i = 0;
while (i < ranges.Length)
{
ranges[i] = new CharacterRange(start + i, 1);
i += 1;
}
format.SetMeasurableCharacterRanges(ranges);
Region[] regions = graphics.MeasureCharacterRanges(_textTOP, _fontTOP, layout, format);
更多精彩
赞助商链接