Archive for tag: Convert Text in to Image using C

Convert Text into Image using C#

Hi there,

I was thinking to do something interesting about embedding fonts to a website. I was trying to embed fonts in website then I thought of converting Text into image here below you can convert the text into image.

    private Bitmap CreateBitmapImage(string TextImage)
    {
        Bitmap objBmp = new Bitmap(1, 1);

        int Width = 0;

        int Height = 0;

        // Create the Font object for the image text drawing.

        Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);

        // Create a graphics object to measure the text's width and height.

        Graphics objGraphics = Graphics.FromImage(objBmp);

        // This is where the bitmap size is determined.

        Width = (int)objGraphics.MeasureString(TextImage, objFont).Width;

        Height = (int)objGraphics.MeasureString(TextImage, objFont).Height;

        // Create the bmpImage again with the correct size for the text and font.

        objBmp = new Bitmap(objBmp, new Size(Width, Height));

        // Add the colors to the new bitmap.

        objGraphics = Graphics.FromImage(objBmp);

        // Set Background color

        objGraphics.Clear(Color.White);

        objGraphics.SmoothingMode = SmoothingMode.AntiAlias;

        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

        objGraphics.DrawString(TextImage, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);

        objGraphics.Flush();

        return (objBmp);
    }

 

If you like this article, please don't forget to like and share with people.

Enjoy your day.