锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

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

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

解决周围限制的方法

I found a way around the limitation :P 我找到一个解决周围限制的方法:P

I made a solution to measuring more then 32 characters

我做了一个解决方案,以测量超过32个字符

using System;
         using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows.Forms;
       using System.Runtime.InteropServices;       
       namespace DrawStringOneCharAtATime
          {
          public partial class Form1 : Form
          {
          public Form1()
         {
          InitializeComponent();
          }    
          private Color RandomColor()
          {
          Random random = new Random();       
          return Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
          }       
          private Color InvertColor(Color color)
          {
          byte bRed = (byte)~(color.R);
          byte bGreen = (byte)~(color.G);
          byte bBlue = (byte)~(color.B);   
          return Color.FromArgb(bRed, bGreen, bBlue);        
          //return Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
          }       
          protected override void OnPaint(PaintEventArgs e)
          {
          base.OnPaint(e);      
          if (this.DesignMode)
          return;       
          using (SolidBrush foreColorSolidBrush = new SolidBrush(this.ForeColor))
          using (SolidBrush backColorSolidBrush = new SolidBrush(Color.Yellow))
          {
          string text = "Never send a human to do a wolf's job cause it would be really really really be funny to watch. :p";      
          RectangleF displayRectangle = new RectangleF(0, 40, 200, 200);     
          //GenericDefault, or new StringFormat() do a better job here then StringFormat.TypoGraphic
          //you also need both these format flags.
          StringFormat strFormat = StringFormat.GenericTypographic;
          strFormat.FormatFlags =
         StringFormatFlags.FitBlackBox | 
          StringFormatFlags.NoClip | //vital or the drawn text will look shredded
          StringFormatFlags.MeasureTrailingSpaces; //so space in end of your text will also get measured      
          CharacterRange[] ranges;
          Region[] charRegions;       
          Color foreColor;
          Color backColor;      
          for (int index = 0, i; index < text.Length; index += 32)
          {
          if (text.Length - index < 1) //if index becomes bigger then the text length
          break;      
          else if (text.Length - index > 32) //if we're guarenteed 32 characters of bliss
          ranges = new CharacterRange[32];      
          else //we got less then 32 characters, but atleast 1
          ranges = new CharacterRange[text.Length - index];      
          //get the character ranges for the 32 char-length or less segment of text we're at
          for (i = 0; i < ranges.Length; i++)
          ranges[i] = new CharacterRange(index + i, 1);  
          //setup for measure
          strFormat.SetMeasurableCharacterRanges(ranges);     
          //measure it
          charRegions = e.Graphics.MeasureCharacterRanges(text, this.Font, displayRectangle, strFormat);      
          //draw text one character at a time
          for (i = 0; i < charRegions.Length; i++)
          {
          //figure out the colors of the text and background
          foreColor = RandomColor();
          backColor = InvertColor(foreColor);       
          foreColorSolidBrush.Color = foreColor;
          backColorSolidBrush.Color = backColor;     
          //draw the background
          e.Graphics.FillRegion(backColorSolidBrush, charRegions[i]);     
          //draw the text 
          RectangleF bounds = charRegions[i].GetBounds(e.Graphics);   

e.Graphics.DrawString(text[index + i].ToString(), this.Font, foreColorSolidBrush, bounds, strFormat); } } } } } }

This is my solution. I thought I should share it as well. 这是我的解决方案。我想我也应该分享它。

I had no need for multi-line text but if you want that you should replace new RectangleF(0, 0, 1000, 1000) with something more suitable for you.

我不需要多行文本,但是如果你想的话,你应该更换新的 RectangleF(0, 0, 1000, 1000),会更适合你。

public static RectangleF[] MeasureString(Graphics graphics, Font font, String text)
{
int max = 32;
int length = text.Length;
RectangleF[] sizes = new RectangleF[length];
for (int j = 0; j < Math.Ceiling(length / (double)max); j++)
{
int max_f = length >= (j + 1) * max ? max : length - j * max;
CharacterRange[] ranges = new CharacterRange[max_f];
for (int i = 0; i < max_f; i++)
{
ranges[i] = new CharacterRange(i + j * max, 1);
}
StringFormat format = new StringFormat(StringFormatFlags.FitBlackBox | StringFormatFlags.NoClip | StringFormatFlags.MeasureTrailingSpaces);
format.SetMeasurableCharacterRanges(ranges);
Region[] regions = graphics.MeasureCharacterRanges(text, font, new RectangleF(0, 0, 1000, 1000), format);
for (int i = 0; i < max_f; i++)
{
RectangleF bounds = regions[i].GetBounds(graphics);
sizes[i + j * max] = bounds;
}
}
return sizes; }

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