none
String drawing RRS feed

  • Question

  • Hello

    I have some questions according PictureBox and drawing text. Why do I get different result in next instances?

                string txt = "some text";
                Font selectedFont = new Font(fontName, 20, GraphicsUnit.Pixel);
                Graphics graph = pictureBox2.CreateGraphics(); // pictureBox2 is a instance of a PictureBox class
                graph.DrawString(txt, selectedFont, Brushes.Black, new Point(2, 2));

    and

                string txt = "some text";

                Font selectedFont = new Font(fontName, 20, GraphicsUnit.Pixel);
                Graphics graph = pictureBox2.CreateGraphics();

                float x = 2F, y = 40F;
                foreach(var ch in txt)
                {
                    string s = ch.ToString();
                    float w = graph.MeasureString(s, selectedFont).Width;
                    graph.DrawString(s, selectedFont, Brushes.Black, new PointF(x, y));
                    x += w;

                }

    There are big margins between symbols in the second instance.

    What should I do so as to get the same results in both cases?

    Thanks in advance for your help

    Sunday, October 30, 2016 6:44 PM

Answers

  • Try an experiment:

    RectangleF r = new RectangleF( x, y, 10000, 10000 );
    for( var i = 0; i < txt.Length; ++i )
    {
        string s = txt.Substring( i, 1 );
    
        CharacterRange[] ranges = new[] { new CharacterRange( i, 1 ) };
        StringFormat format = new StringFormat();
        format.SetMeasurableCharacterRanges( ranges );
        var regions = graph.MeasureCharacterRanges( txt, selectedFont, r, format );
    
        PointF p = regions[0].GetBounds( graph ).Location;
    
        graph.DrawString( s, selectedFont, Brushes.Black, p );
    }

    It can be optimized. For example, all of the regions can be obtained using a single call to MeasureCharacterRanges if you pass a corresponding CharacterRange array.

    • Proposed as answer by Kristin Xie Wednesday, November 2, 2016 5:37 AM
    • Marked as answer by Iurii Onishchenko Wednesday, November 2, 2016 8:30 PM
    Sunday, October 30, 2016 7:29 PM

All replies

  • Try an experiment:

    RectangleF r = new RectangleF( x, y, 10000, 10000 );
    for( var i = 0; i < txt.Length; ++i )
    {
        string s = txt.Substring( i, 1 );
    
        CharacterRange[] ranges = new[] { new CharacterRange( i, 1 ) };
        StringFormat format = new StringFormat();
        format.SetMeasurableCharacterRanges( ranges );
        var regions = graph.MeasureCharacterRanges( txt, selectedFont, r, format );
    
        PointF p = regions[0].GetBounds( graph ).Location;
    
        graph.DrawString( s, selectedFont, Brushes.Black, p );
    }

    It can be optimized. For example, all of the regions can be obtained using a single call to MeasureCharacterRanges if you pass a corresponding CharacterRange array.

    • Proposed as answer by Kristin Xie Wednesday, November 2, 2016 5:37 AM
    • Marked as answer by Iurii Onishchenko Wednesday, November 2, 2016 8:30 PM
    Sunday, October 30, 2016 7:29 PM
  • Thanks for help. It is helpful information for me.

    Wednesday, November 2, 2016 8:30 PM