锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 英语翻译 / 使用MeasureCharacterRanges逐字符绘制字符串
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
技术分类
讨论组翻译
解决周围限制的方法
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。

使用MeasureCharacterRanges逐字符绘制字符串

效果截图

Introduction 介绍

This article will demonstrate the use of MeasureCharacterRanges to draw a string a character at a time. Not very exciting, I hear you cry. Bear with me.

本文将演示使用MeasureCharacterRanges逐字符绘制字符串。我已经听到你尖叫了,但不要太兴奋。请耐心等待。

Background 背景

I wrote this short article after seeing a question posted on a newsgroup about being able to draw text along a curve. While drawing a character at a time is easy, I wanted to make sure the character spacing was maintained.

我写这篇简短的文章是因为,看到在新闻讨论组张贴的一个问题,关于能够沿着一条曲线绘制文本的问题。虽然有时绘制一个字符是很容易的,但我想确保字符的间距。

The code 代码

Just set up some text to work with: 设置一些文本处理:

string measureString = "This is a test string.";
int numChars = measureString.Length;

Initialize the character ranges array, this is used to delimit the blocks of characters in the string, in this example, each character is a 'range'.

初始化字符数组的范围,这在字符串中用来分隔字符块,在这个例子中,每个字符都是一个“范围”。

//
// Set up the characted ranger array.

Now, initialize the StringFormatFlags, I'm using the NoClips flag to ensure that the character is not clipped when drawing.

现在,初始化StringFormatFlags ,我使用NoClips标志,以确保在绘制时字符没有被省略一部分。

//
// Set up the string format
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.NoClip;
stringFormat.SetMeasurableCharacterRanges(characterRanges);

Set up an array to hold the calculated regions: 建立一个数组来保存计算区域:

//
// Set up the array to accept the regions.
Region[] stringRegions = new Region[numChars];
for(int i = 0; i<numChars; i++)
characterRanges[i] = new CharacterRange(i, 1);

Create a font, and use MeasureCharacterRanges() to calculate the regions for the character ranges.

创建一个字体,并使用MeasureCharacterRanges()来计算字符的区域范围。

The regions returned by MeasureCharacterRanges are converted to rectangles by using the GetBounds() function. The rectangle can then be manipulated using offset or any other method to adjust its placement.

通过MeasureCharacterRanges返回的区域是通过使用getBounds()函数被转换为矩形。矩形可以使用偏移或其他可以调整布局的方法来操纵。

In this example, I offset the Y position using a random amount to give wavy text.

在这个例子中,我使用随机量给出一个波浪形的文本,偏移Y轴的位置。

//
   // The font to use.. 'using' will dispose of it for us
   using (Font stringFont = new Font("Times New Roman", 16.0F))
   {     
   //
   // Get the max width.. for the complete length
    SizeF size = g.MeasureString(measureString, stringFont );      
    //
   // Assume the string is in a stratight line, just to work out the 
   // regions. We will adjust the containing rectangles later.
    RectangleF layoutRect = 
   new RectangleF( 0.0f, 0.0f, size.Width, size.Height);       
    //
   // Caluclate the regions for each character in the string.
   stringRegions = g.MeasureCharacterRanges(
   measureString,
   stringFont,
   layoutRect,
   stringFormat);       
    //
   // Some random offsets, uncomment the DrawRectagle
  // if you want to see the bounding box.
  Random rand = new Random();
  for ( int indx = 0 ; indx < numChars; indx++ )
  {
   Region region = stringRegions[indx] as Region;
  RectangleF rect = region.GetBounds(g);
  rect.Offset( 0f, (float) rand.Next(100) / 10f);
  g.DrawString( measureString.Substring(indx,1), 
  stringFont, Brushes.Yellow, rect, stringFormat );
  // g.DrawRectangle( Pens.Red, Rectangle.Round( rect ));
   }
 }

As I said at the beginning, this is not the most efficient code, the calculations could be done outside the drawing routine and cached. However, I hope this demonstrates how easy it is to determine the character positions.

正如我在开始时说,这还不是最高效的代码,计算可以在绘图程序和缓存的范围之外被执行。然而,我希望这演示了确定字符位置是多么容易。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内