Shubhit304
Shubhit304

Reputation: 301

Generate image from text

I want to generate the image from text so I found one code from previous questions as below

/// <summary>
/// Creates an image containing the given text.
/// NOTE: the image should be disposed after use.
/// </summary>
/// <param name="text">Text to draw</param>
/// <param name="fontOptional">Font to use, defaults to Control.DefaultFont</param>
/// <param name="textColorOptional">Text color, defaults to Black</param>
/// <param name="backColorOptional">Background color, defaults to white</param>
/// <param name="minSizeOptional">Minimum image size, defaults the size required to display the text</param>
/// <returns>The image containing the text, which should be disposed after use</returns>
public static Image DrawText(string text, Font fontOptional=null, Color? textColorOptional=null, Color? backColorOptional=null, Size? minSizeOptional=null)
{
    Font font = Control.DefaultFont;
    if (fontOptional != null)
        font = fontOptional;

    Color textColor = Color.Black;
    if (textColorOptional != null)
        textColor = (Color)textColorOptional;

    Color backColor = Color.White;
    if (backColorOptional != null)
        backColor = (Color)backColorOptional;

    Size minSize = Size.Empty;
    if (minSizeOptional != null)
        minSize = (Size)minSizeOptional;

    //first, create a dummy bitmap just to get a graphics object
    SizeF textSize;
    using (Image img = new Bitmap(1, 1))
    {
        using (Graphics drawing = Graphics.FromImage(img))
        {
            //measure the string to see how big the image needs to be
            textSize = drawing.MeasureString(text, font);
            if (!minSize.IsEmpty)
            {
                textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width;
                textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height;
            }
        }
    }

    //create a new image of the right size
    Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height);
    using (var drawing = Graphics.FromImage(retImg))
    {
        //paint the background
        drawing.Clear(backColor);

        //create a brush for the text
        using (Brush textBrush = new SolidBrush(textColor))
        {
            drawing.DrawString(text, font, textBrush, 0, 0);
            drawing.Save();
        }
    }
    return retImg;
}

This code is perfectly working. Also it handles the minimum size you want to create.

Now I also want to handle the maximum width of image to be handled like if I pass maxWidth of image as say 30 characters, then it will create image with less than 30 characters according to its width, but if any line has more than 30 characters then the characters above 30 should come in next line first.

For example- If I need to create image of following data with maxWidth 50 characters:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Then it should create the image as below:

Lorem Ipsum is simply dummy text of the printing a
nd typesetting industry. Lorem Ipsum has been the 
industry's standard dummy text ever since the 1500
s, when an unknown printer took a galley of type a
nd scrambled it to make a type specimen book.

Upvotes: 2

Views: 1279

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112402

The methods MeasureString and DrawString have overloads allowing you to specify the size of the text. These methods automatically wrap the text according to the given width.

drawing.TextRenderingHint = TextRenderingHint.AntiAlias; // Improves quality.
var rect = new RectangleF(0, 0, width, height); // Specify maximum width and height.
SizeF textSize = drawing.MeasureString(text, font, rect.Size);
// Use textSize to do calculations.
drawing.DrawString(text, font, textBrush, rect);

Yet another overload of MesaureString allows you to specify only the with. Both methods have overloads with a StringFormat parameter giving even more control over things like spacing and alignment.

Setting drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; significantly improves quality.

Upvotes: 1

Related Questions