Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Convert Text to Image

5.00/5 (2 votes)
27 Nov 2011CPOL 9.5K   1  
I'd like to offer a funny alternative by using a dummy TextBox. public static Bitmap GetPlainTextBitmap(string strText, Font font, Color colorBack, Color colorText, Size sizeTargetBitmap) { // Create a dummy TextBox TextBox txDummy = new TextBox(); ...
I'd like to offer a "funny" alternative by using a dummy TextBox.

C#
public static Bitmap GetPlainTextBitmap(string strText, Font font, Color colorBack, Color colorText, Size sizeTargetBitmap)
        {
            // Create a dummy TextBox 
            TextBox txDummy = new TextBox();
            txDummy.BorderStyle = BorderStyle.None;
            txDummy.Multiline = true;
            // ... and initialize with the given parameters
            txDummy.Size = sizeTargetBitmap;
            txDummy.Text = strText;
            txDummy.Font = font;
            txDummy.BackColor = colorBack;
            txDummy.ForeColor = colorText;

            // Now just draw the dummy TextBox to an bitmap
            Bitmap bmp = new Bitmap(txDummy.Width, txDummy.Height);
            txDummy.DrawToBitmap(bmp, txDummy.ClientRectangle);

            return bmp;
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)