drawString in growing/shrinking rectangle

  • Thread starter Thread starter Tim Bücker
  • Start date Start date
T

Tim Bücker

Hello.

The MeasureString method is quite slow so I am wondering how one can
efficently draw a string in a growing or shrinking rectangle.
In other words; having a huge rectangle the font size should be for example
100; having a small rectangle the font size should be 8.

Any ideas?
Thanks and greetings,
Tim.
 
Tim,

You shouldn't be setting the point size yourself for things like this.
If you want the most in performance, then you should be rendering the fonts
yourself. Basically, you should have your text, and get all the points on
the letters that the font dictates. Once you have that, you would be
responsible for drawing the lines between the points. Basically, you are
rendering the font.

You should look for an OpenGl interface for .NET (there is one out
there), or find something similar in DirectX. These will allow you to
render the fonts, and scale them appropriately.

Hope this helps.
 
Nicholas Paldino said:
You shouldn't be setting the point size yourself for things like this.
If you want the most in performance, then you should be rendering the fonts
yourself. Basically, you should have your text, and get all the points on
the letters that the font dictates. Once you have that, you would be
responsible for drawing the lines between the points. Basically, you are
rendering the font.

Hello and thanks for answering!
Of course you are right; most performance is achieved using a hardware
accelerated api.
But that is some kind of oversized for my program as I only need this little
feature once or twice.

At the moment I am doing it with a recursive function testing if the font
size is ok or not
using some kind of divide and conquer algorithm to get the most suitable
font size.
Enough performance for smaller apps.

Thanks again for answering!
Greetings,
Tim.
 
Tim Bücker said:
"Nicholas Paldino [.NET/C# MVP]" wrote:
At the moment I am doing it with a recursive function testing if the font
size is ok or not
using some kind of divide and conquer algorithm to get the most suitable
font size.
Enough performance for smaller apps.

I do the same sort of thing but in a different manner.
I pass a string, font, rectangle and graphics to a method which returns a
fontsize
I get the size of the string.
Check if the width is greater than the size of the rectangle.
If it is then I get the percentage greater and resize down by that percent.

Then I get the size again and if the height is greater than the rectangle
then I get the percentage height diff and resize down again.

I only need to ensure that the font size is not "greater" than will fit in
the rectangle though. I dont care if it is "smaller".
Like you, I only use this quite infrequently and it works quite well.
HTH
JB
 
Back
Top