title: How to fix this Spacement String Method with Break Line and FormatString.Alignment
body: <p>Well, I'm trying to create a method that make a drawString with a custom spacement.
  For 1 line this is perfectly ready, but when I have a break line <code>\n</code> the next line start in the middle and I don't know why. (This method needs to work in a string with 32+ chars).</p>
  
  <p>And when I put <code>formatString.Alignment = StringAlignment.Center</code> and <code>formatString.LineAlignment = StringAlignment.Center</code>the string don't centralize and I don't know why again.</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Collections.Generic;
  
  namespace TryApp
  {
      /// &lt;summary&gt;
      /// Summary description for Form1.
      /// &lt;/summary&gt;
      public class Form1 : Form
      {
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.Container components = null;
  
          public Form1()
          {
              //
              // Required for Windows Form Designer support
              //
              InitializeComponent();
  
              //
              // TODO: Add any constructor code after InitializeComponent call
              //
          }
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          protected override void Dispose( bool disposing )
          {
              if( disposing )
              {
                  if (components != null) 
                  {
                      components.Dispose();
                  }
              }
              base.Dispose( disposing );
          }
  
          #region Windows Form Designer generated code
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent()
          {
              this.SuspendLayout();
              // 
              // Form1
              // 
              this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
              this.ClientSize = new System.Drawing.Size(827, 292);
              this.Name = "Form1";
              this.Text = "Form1";
              this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
              this.ResumeLayout(false);
  
          }
          #endregion
  
          /// &lt;summary&gt;
          /// The main entry point for the application.
          /// &lt;/summary&gt;
          [STAThread]
          static void Main() 
          {
              Application.Run(new Form1());
          }
  
          private void Form1_Paint(object sender, PaintEventArgs e)
          {
              MeasureCharacterRangesRegions(e);
          }
  
          public void MeasureCharacterRangesRegions(PaintEventArgs e)
          {
              //
              // Get a more convenient graphics object.
              Graphics g = e.Graphics;
  
              // Set up string.
              string measureString            = "This is a test string and \n We have a break line.";
              int    numChars                 = measureString.Length;
              int numCharsLeft = measureString.Length;
              double numRanges                = Math.Ceiling((double) numChars / 32);
              Font stringFont = new Font("Times New Roman", 16.0F);
  
              List&lt;StringFormat&gt; stringFormats = new List&lt;StringFormat&gt;();
              List&lt;CharacterRange[]&gt; characterRanges = new List&lt;CharacterRange[]&gt;();
  
              for (int x = 0; x &lt; numRanges; x++) {
  
                  characterRanges.Add(new CharacterRange[numCharsLeft &lt;= 32 ? numCharsLeft : 32]);
                  numCharsLeft -= numCharsLeft &lt;= 32 ? numCharsLeft : 32;
  
                  for (int i = 0; i &lt; characterRanges[x].Length; i++)
                      characterRanges[x][i] = new CharacterRange((x * 32) + i, 1);
  
                  StringFormat stringFormat = new StringFormat();
                  stringFormat.FormatFlags = StringFormatFlags.NoClip;          // Make sure the characters are not clipped
                  stringFormat.SetMeasurableCharacterRanges(characterRanges[x]);
                  stringFormats.Add(stringFormat);
  
              }
  
              SizeF size = g.MeasureString(measureString, stringFont);
              RectangleF layoutRect = new RectangleF(0.0f, 0.0f, size.Width, size.Height);
  
              List&lt;Region[]&gt; regions = new List&lt;Region[]&gt;();
  
              for(int i = 0; i &lt; characterRanges.Count; i++) {
                  regions.Add(new Region[characterRanges[i].Length]);
                  regions[i] = e.Graphics.MeasureCharacterRanges(
                  measureString,
                  stringFont,
                  layoutRect,
                  stringFormats[i]);
              }
  
              for ( int i = 0 ; i &lt; numChars; i++ )
              {
                  int block = (int) Math.Floor((double) i / 32);
                  Region region = regions[block][i - (32 * block)] as Region;
                  RectangleF rect = region.GetBounds(g);
                  rect.Offset( i * 10, 0);
                  g.DrawString( measureString.Substring(i,1), stringFont, Brushes.Black, rect, stringFormats[block]);
                  // g.DrawRectangle( Pens.Red, Rectangle.Round( rect ));
              }
          }
      }
  }
  </code></pre>
  
  <p>I'm adaptating this code: <a href="https://www.codeproject.com/Articles/7375/Using-MeasureCharacterRanges-to-Draw-Text" rel="nofollow noreferrer">https://www.codeproject.com/Articles/7375/Using-MeasureCharacterRanges-to-Draw-Text</a> (This don't work with strings that have more then 32 chars)</p>