Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Convert Text to Image

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
27 Nov 2011CPOL 9.3K   1   3  
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)


Written By
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --