Measure a String Without Using a Graphics Object

Measure a String without using a Graphics object?

If you have a reference to System.Windows.Forms, try using the TextRenderer class. There is a static method (MeasureText) which takes the string and font and returns the size. MSDN Link

Custom Measure String in C# without Graphics.MeasureString

highestWidth = redPixelMatrix.Keys.Count; This will just count the number of columns containing red pixels, excluding any spaces in the text. You presumably want the minimum and maximum indices.

I.e.

var minX = int.MaxValue;
var maxX = int.MinValue;
// Loops over rows & columns
// Check if pixel is red
if(i > maxX) maxX = i;
if(i < minX) minX = i;

If you only want the text width and not the bounds you can just do maxX - minX.

Is there a way to measure the amount of a string that will fit into a printed area without using the graphics object?

Yes, there is, in .NET 2.0

Size size = TextRenderer.MeasureString(this.Text, this.Font);

How can I get the dimensions of a drawn string without padding?

I think it is because the font height is 46 for 30point font.

This is so you can have special uppercase characters (ÉÖÄÅ) and lover case characters(gqp).

The leading and trailing blank spaces probably also have some thing to do with this.

How to determine the length of a graphic string?

This thread explains how to do it:
Calculate the display width of a string in Java

You should first get the font metrics, and then ask the metrics how wide a certain string is.

Calculate the display width of a string in Java

If you just want to use AWT, then use Graphics.getFontMetrics (optionally specifying the font, for a non-default one) to get a FontMetrics and then FontMetrics.stringWidth to find the width for the specified string.

For example, if you have a Graphics variable called g, you'd use:

int width = g.getFontMetrics().stringWidth(text);

For other toolkits, you'll need to give us more information - it's always going to be toolkit-dependent.

How to get string's width

MeasureString is not a static method. You will need to use a Graphics instance to access it.

For example:

private void MeasureString(PaintEventArgs e)
{
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
}

If you are referencing System.Windows.Forms use the TextRenderer class instead, this will relieve you of having a Graphics object.

private void MeasureText()
{
String text1 = "Some Text";
Font arialBold = new Font("Arial", 16);
Size textSize = TextRenderer.MeasureText(text1, arialBold);
}

UPDATE:

You can use a fake image to measure a string using Graphics as we cannot use CreateGraphics in a class library:

private void MeasureString()
{
string measureString = "Measure String";
Font font = new Font("Arial", 16);
Image fakeImage = new Bitmap(1,1);
Graphics graphics = Graphics.FromImage(fakeImage);
SizeF size = graphics.MeasureString(measureString, font);
}

What happens when Graphics.MeasureString() is called?

public SizeF MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat) 
{
if (text == null || text.Length == 0)
{
return new SizeF(0, 0);
}

if (font == null)
{
throw new ArgumentNullException("font");
}

GPRECTF grfLayout = new GPRECTF(0, 0, layoutArea.Width, layoutArea.Height);
GPRECTF grfboundingBox = new GPRECTF();

int a, b;
int status = SafeNativeMethods.Gdip.GdipMeasureString(new HandleRef(this, this.NativeGraphics), text, text.Length, new HandleRef(font, font.NativeFont),
ref grfLayout,
new HandleRef(stringFormat, (stringFormat == null) ? IntPtr.Zero : stringFormat.nativeFormat),
ref grfboundingBox, out a, out b);

if (status != SafeNativeMethods.Gdip.Ok)
{
throw SafeNativeMethods.Gdip.StatusException(status);
}

return grfboundingBox.SizeF;
}

So no, no rendering occurs. Even the documentation says so:

Measures the specified string when drawn with the specified Font.



Related Topics



Leave a reply



Submit