The MeasureString Method in GDI+

In this article I will explain about MeasureString Method in GDI+.


This article has been excerpted from book "Graphics Programming with GDI+".

MeasureString measure a string when it is drawn with a font object and returns the size of the string as a SizeF object. You can use SizeF to find out the height and width of string.

MeasureString can also be used to find the total numbers of characters and lines in a string. It has seven overloaded methods. It takes two required parameters: the string and font to measure. Optional parameters you can pass include the width of the string in pixels, maximum layout area of the text, string format, and combinations of these parameters.

Listing 3.30 uses the MeasureString method to measure a string's height and width and draws a rectangle and a circle around the string. This example also shows how to find the total number of lines and character of a string.

LISTING 3.30: Using the MeasureString method


Graphics
g = Graphics.FromHwnd(this.Handle);
            g.Clear(this.BackColor);
            string testString = "This is a test string";
            Font verdanathe-measurestring-method-in-gdi = new Font("Verdana", the-measurestring-method-in-gdi);
            Font tahoma18 = new Font("Tahoma", 18);

            int nChars;
            int nLines;

            // Call MeasureString to measure a string
            SizeF sz = g.MeasureString(testString, verdanathe-measurestring-method-in-gdi);
            string stringDetails = "Height : " + sz.Height.ToString() + ", Width: " + sz.Width.ToString();
            MessageBox.Show("first string detials : " + stringDetails);
            g.DrawString(testString, verdanathe-measurestring-method-in-gdi, Brushes.Green, new PointF(0, 100));
            g.DrawRectangle(new Pen(Color.Red, 2), 0.0F, 100.0F, sz.Width, sz.Height);
            sz = g.MeasureString("Ellipse", tahoma18, new SizeF(0.0F, 100.0F), new StringFormat(), out nChars, out nLines);
            stringDetails = "Height : " + sz.Height.ToString()
            + ", width : " + sz.Width.ToString()
            + ", Lines : " + nLines.ToString()
            + ", Chars : " + nChars.ToString();
            MessageBox.Show("Second string details : " + stringDetails);
             g.DrawString("Ellipse", tahoma18, Brushes.Blue, new PointF(10, 10));
            g.DrawEllipse(new Pen(Color.Red, 3), 10, 10,
            sz.Width, sz.Height);
            g.Dispose();


Figure 3.41 shows the output from Listing 3.30.

figure_new1.gif

figure_new2.gif

FIGURE 3.41: Using MeasureString when drawing text.

Conclusion

Hope the article would have helped you in understanding MeasureString Method in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.