Sree Kanth
Sree Kanth

Reputation: 31

Text added to image cuts off at the edges

I am trying to add text over an image, and it works good if the text is short. However, if I add longer text it will be cut off at the edge.

I need the text to come to a new line if it's too long.

Here's the code I have so far:

Graphics graphicImage = Graphics.FromImage(bitMapImage);

//Smooth graphics is nice.
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
Font font = new Font("Tahoma", 6);
int x_axis = 50;
int y_axis = 120;
int distance = 50;
graphicImage.DrawString("This is a very long text and this long text might come to a new line below", font, Brushes.Yellow, new PointF(x_axis, y_axis + distance * 2));

String tempFile = folder + "output.jpeg";
bitMapImage.Save(tempFile, ImageFormat.Jpeg);

And here is the image it creates Image with text overlay

How can I make the text wrap?

Upvotes: 3

Views: 147

Answers (1)

John Larson
John Larson

Reputation: 31

Draw text in a rectangle, and it will wrap:

string text1 = "Draw `enter code here`text in a rectangle by passing a RectF to the DrawString method.";
using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
    RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
    e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
    e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
}

Upvotes: 3

Related Questions