Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill text inside rectangle

I'm using GDI+ to draw a string on a Graphics object.

I want the string to fit inside a pre-defined rectangle (without breaking any lines)

Is there's anyway of doing this besides using TextRenderer.MeasureString() in a loop until the desirable size is returned?

something like:

DrawScaledString(Graphics g, string myString, Rectangle rect)
like image 266
Nissim Avatar asked Nov 11 '10 07:11

Nissim


People also ask

How do you fill in a rectangle in illustrator?

Type into the rectangle. The rectangle disappears. Select the Direct Selection tool. Click on the edge of the text object (the disappeared rectangle). Give it a fill and or stroke.

How do I change the text inside a rectangle?

You can use the Direct Selection tool (White Arrow) to select the rectangle and give it a Fill and Stroke. If you want to modify the placement of the text inside the rectangle, choose: Type Area Type Options... Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.

How do I put text inside a shape?

Notice that in your Layers panel the first Rectangle was converted to text inside a shape.One way to do it is this:Copy the rectangle.Create the textPaste in backThe text will be touching the edges of the path, though, and one or the other will need to be sized. InDesign does this a little better.

How to draw text in a rectangle in Java?

string text2 = "Draw text in a rectangle by passing a RectF to the DrawString method."; using (Font font2 = new Font ("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)) { Rectangle rect2 = new Rectangle (30, 10, 100, 122); // Specify the text is wrapped.


2 Answers

You can use the ScaleTransform

string testString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse et nisl adipiscing nisl adipiscing ultricies in ac lacus.
Vivamus malesuada eros at est egestas varius tincidunt libero porttitor.
Pellentesque sollicitudin egestas augue, ac commodo felis ultricies sit amet.";

Bitmap bmp = new Bitmap(300, 300);
using (var graphics = Graphics.FromImage(bmp))
{
    graphics.FillRectangle(Brushes.White, graphics.ClipBounds);
    var stringSize = graphics.MeasureString(testString, this.Font);
    var scale = bmp.Width / stringSize.Width;
    if (scale < 1)
    {
        graphics.ScaleTransform(scale, scale);
    }
    graphics.DrawString(testString, this.Font, Brushes.Black, new PointF());
}
bmp.Save("lorem.png", System.Drawing.Imaging.ImageFormat.Png);

But you might get some alias effects.

alt text

Edit:

But if you want to change the font size instead I guess you can change the font size with scale in the code above instead of using the scale transform. Try both and compare the quality of the result.

like image 114
Albin Sunnanbo Avatar answered Sep 28 '22 11:09

Albin Sunnanbo


Here's another solution to the problem, it's a little intensive as it requires a fair bit of font creation and destruction, but may work better, depending on your circumstances and needs:

public class RenderInBox
{
    Rectangle box; 
    Form root;
    Font font;
    string text;

    StringFormat format;

    public RenderInBox(Rectangle box, Form root, string text, string fontFamily, int startFontSize = 150)
    {
        this.root = root;
        this.box = box;
        this.text = text;

        Graphics graphics = root.CreateGraphics();

        bool fits = false;
        int size = startFontSize;
        do
        {
            if (font != null)
                font.Dispose();

            font = new Font(fontFamily, size, FontStyle.Regular, GraphicsUnit.Pixel);

            SizeF stringSize = graphics.MeasureString(text, font, box.Width, format);

            fits = (stringSize.Height < box.Height);
            size -= 2;
        } while (!fits);

        graphics.Dispose();

        format = new StringFormat()
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center
        };

    }

    public void Render(Graphics graphics, Brush brush)
    {
        graphics.DrawString(text, font, brush, box, format);
    }
}

To use it simply create a new class and call Render(). Note this is specifically written for rendering to a form.

var titleBox = new RenderInBox(new Rectangle(10, 10, 400, 100), thisForm, "This is my text it may be quite long", "Tahoma", 200);
titleBox.Render(myGraphics, Brushes.White);

You should create the RenderInBox object upfront due to it's intensive creation nature. Therefore it's not suitable for every need.

like image 44
Rob Avatar answered Sep 28 '22 09:09

Rob