title: How can I draw multi-colored text using graphics class on panel?
body: <p>I want to draw the following text on panel:<br></p>
  
  <p><img src="https://i.stack.imgur.com/qEiGR.png" alt="sample text"></p>
  
  <p>It is a multi-colored text.</p>
  
  <p>I found this <a href="http://csharphelper.com/blog/2014/08/draw-text-in-random-colors-in-c/" rel="nofollow noreferrer">article</a> about drawing multicolored text. </p>
  
  <p>I replaced characters with words but it doesn't work.<br></p>
  
  <p><img src="https://i.stack.imgur.com/juFJLt.png" alt="output-screenshot"></p>
  
  <p>(I use FillPath/DrawPath to draw text)</p>
  
  <p><strong>my code:</strong></p>
  
  <pre><code>private void Form1_Paint(object sender, PaintEventArgs e)
      {
          const string txt = "C# Helper! Draw some text with each letter in a random color.";
  
          // Make the font.
          using (Font the_font = new Font("Times New Roman", 40,
              FontStyle.Bold | FontStyle.Italic))
          {
              // Make a StringFormat object to use for text layout.
              using (StringFormat string_format = new StringFormat())
              {
                  // Center the text.
                  string_format.Alignment = StringAlignment.Center;
                  string_format.LineAlignment = StringAlignment.Center;
                  string_format.FormatFlags = StringFormatFlags.NoClip;
  
                  // Make CharacterRanges to indicate which
                  // ranges we want to measure.
  
                  MatchCollection mc = Regex.Matches(txt, @"[^\s]+");
                  CharacterRange[] ranges = new CharacterRange[mc.Count];
                  int g = 0;
                  foreach (Match m in mc)
                  {
                      ranges[g] = new CharacterRange(m.Index, m.Length);
                      g++;
                  }
                  string_format.SetMeasurableCharacterRanges(ranges);
  
                  // Measure the text to see where each character range goes.
                  Region[] regions =
                      e.Graphics.MeasureCharacterRanges(
                          txt, the_font, this.ClientRectangle,
                          string_format);
  
                  // Draw the characters one at a time.
                  for (int i = 0; i &lt; ranges.Length; i++)
                  {
                      // See where this character would be drawn.
                      RectangleF rectf = regions[i].GetBounds(e.Graphics);
                      Rectangle rect = new Rectangle(
                          (int)rectf.X, (int)rectf.Y,
                          (int)rectf.Width, (int)rectf.Height);
  
                      // Make a brush with a random color.
                      using (Brush the_brush = new SolidBrush(RandomColor()))
                      {
                          // Draw the character.
                          string txts = txt.Substring(ranges[i].First, ranges[i].Length);
                          e.Graphics.DrawString(txts,
                              the_font, the_brush, rectf, string_format);
                      }
                  }
              }
          }
      }
  </code></pre>
  
  <p>What is the problem?</p>