measure string
-
hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).
-
hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).
TextRenderer should work for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.measuretext.aspx[^]
-
TextRenderer should work for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.measuretext.aspx[^]
-
hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).
You don't need to receive a Graphics object to use one in your method. What you could do is create a 1 by 1 bitmap, and then retrieve the Graphics object from that.
using (Bitmap img = new Bitmap(1,1))
{
float resolution = 300;
img.SetResolution(resolution, resolution);
using (Graphics g = Graphics.FromImage(img))
{
// We have a graphics object that we can use now.
}
}"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).
The Graphics context dictates the way the Fonts are rendered. It is not possible to accurately measure a string without knowing where it is going. All other option require guessing in the method. If you want to guess you can look-up the font characteristics and roll your own, or as suggested earlier create a fake graphics context that does not match the device. Unfortunately, it will be buggy in some edge cases that may be hard to detect in development but the client will notice immediately. The best thing to do is to pass a reference to the Graphics context everywhere you want to draw to a device.
Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane