Search code examples
.netdrawstring

Resize Rectangle based on text


I am working on a custom control where I am using Rectangle to show some data. Now, lets say the width and height of the rectangle is set to 100.

If I have less amount of data, then its fine, I can make it draw using DrawString method. But, sometimes Data is bigger and so it gets clipped.

I have tried using MeasureString Method, but its not retrieving the correct values. Is there any way I can see what will be the size of the string, [both length and Height], if it has to be shown in 100px width rectangle. I mean the height can be increased but not width, so that I can use the correct height of the string to make it appear in full in that rectangle.


Solution

  • Set the Height of the rectangle to 0. And then after we do MeasureString, we can find the height of the string which can be placed withing that rectangle of specified width. give that height to rectangle to draw and then draw the string in it.

    http://www.tigraine.at/2009/01/03/gdi-drawing-string-with-word-wrap/