Click here to Skip to main content
15,171,851 members
Articles / Programming Languages / C#
Posted 19 Apr 2011

Tagged as


28 bookmarked

Convert Text to Image

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
17 May 2011CPOL
Convert Text to Image
We can easily convert text to image in C# by using System.Drawing namespace. Here these tips will guide you how to create bitmap image from text. It’s using Graphics property of System.Drawing namespace to convert text to bitmap image and place the image in a picture box. The Graphics class provides methods for drawing to the display device.
In this article, you will see a method Convert_Text_to_Image(string txt, string fontname, int fontsize) is responsible to return Bitmap Image on the basis of Text, fontname and fontsize.
Take a look at how to use the mentioned method for converting Text to Image:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Text_to_Image
    public partial class Form1 : Form
        public Form1()

        public static Bitmap Convert_Text_to_Image(string txt, string fontname, int fontsize)
            //creating bitmap image
            Bitmap bmp = new Bitmap(1, 1);
            //FromImage method creates a new Graphics from the specified Image.
            Graphics graphics = Graphics.FromImage(bmp);
            // Create the Font object for the image text drawing.
            Font font = new Font(fontname, fontsize);
           // Instantiating object of Bitmap image again with the correct size for the text and font.
            SizeF stringSize = graphics.MeasureString(txt, font);
            bmp = new Bitmap(bmp,(int)stringSize.Width,(int)stringSize.Height); 
            graphics = Graphics.FromImage(bmp);

           /* It can also be a way
          bmp = new Bitmap(bmp, new Size((int)graphics.MeasureString(txt, font).Width, (int)graphics.MeasureString(txt, font).Height));*/

            //Draw Specified text with specified format 
            graphics.DrawString(txt, font, Brushes.Red, 0, 0);
            return bmp;     //return Bitmap Image 

        private void btnconvert_Click(object sender, EventArgs e)
           picbox.Image = Convert_Text_to_Image(txtvalue.Text, "Bookman Old Style", 20); // Passing appropriate value to Convert_Text_to_Image method 
           picbox.SizeMode = PictureBoxSizeMode.StretchImage;

Now write something in Textbox and then press convert button to see the desired output.
Go there[^] to download source code.

[Edited] Another approach
public Bitmap ConvertTextToImage(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int width, int Height)
            Bitmap bmp = new Bitmap(width, Height);
            using (Graphics graphics = Graphics.FromImage(bmp))

                Font font = new Font(fontname, fontsize);
                graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
                graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);

            return bmp;

Take a look how to use Method in same Project.
private void btnconvert_Click(object sender, EventArgs e)
           //You can pass value in parameter as per your requirement.
        picbox.Image = this.ConvertTextToImage(txtvalue.Text, "Bookman Old Style", 10, Color.Yellow, Color.Red, txtvalue.Width, txtvalue.Height);

Go there[^] to Download Edited Source Code.


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


About the Author

Software Developer
India India
No Biography provided

Comments and Discussions

QuestionNice solution Pin
Misael C. Homem8-Nov-19 2:46
MemberMisael C. Homem8-Nov-19 2:46 
QuestionRTL text Pin
WithoutBrain199424-Dec-14 23:50
MemberWithoutBrain199424-Dec-14 23:50 
GeneralMy vote of 3 Pin
A. Najafzadeh12-Jul-14 19:25
MemberA. Najafzadeh12-Jul-14 19:25 
SuggestionTheres a better alternate Pin
SumantaBanerjje23-Mar-14 9:00
MemberSumantaBanerjje23-Mar-14 9:00 
GeneralRe: Theres a better alternate Pin
Ami_Modi8-Apr-14 1:43
MemberAmi_Modi8-Apr-14 1:43 
QuestionWorks Great Pin
pgmr_6480422-Nov-13 12:00
Memberpgmr_6480422-Nov-13 12:00 
Thanks...this worke great for me Smile | :)
Questiondifferent iamge file formats Pin
random413-Jan-13 23:07
Memberrandom413-Jan-13 23:07 
AnswerRe: different iamge file formats Pin
SumantaBanerjje23-Mar-14 9:02
MemberSumantaBanerjje23-Mar-14 9:02 
GeneralRe: But still 3 points(by Luc) pending. Read his 3rd point again... Pin
thatraja20-Apr-11 6:40
professionalthatraja20-Apr-11 6:40 
GeneralI will use this answer in Q & A for all those asking for "Co... Pin
Tarun.K.S21-Apr-11 1:21
MemberTarun.K.S21-Apr-11 1:21 
GeneralRe: Thanks Tarun :) Pin
RaviRanjanKr21-Apr-11 3:10
professionalRaviRanjanKr21-Apr-11 3:10 
GeneralGood one. My 5 Pin
Toniyo Jackson20-Apr-11 22:12
MemberToniyo Jackson20-Apr-11 22:12 
GeneralRe: Thanks Toniyo :) Pin
RaviRanjanKr20-Apr-11 23:06
professionalRaviRanjanKr20-Apr-11 23:06 
GeneralNice. +5ed :) Pin
Kunal Chowdhury «IN»20-Apr-11 9:00
professionalKunal Chowdhury «IN»20-Apr-11 9:00 
GeneralRe: Thanks! Pin
RaviRanjanKr20-Apr-11 9:47
professionalRaviRanjanKr20-Apr-11 9:47 
GeneralI second thatraja. And I also think the Font should be a par... Pin
Niklas L20-Apr-11 2:07
MemberNiklas L20-Apr-11 2:07 
GeneralI second Luc. Pin
thatraja19-Apr-11 16:29
professionalthatraja19-Apr-11 16:29 
GeneralRe: Now Edited Take a look :) Pin
RaviRanjanKr20-Apr-11 4:08
professionalRaviRanjanKr20-Apr-11 4:08 
GeneralRe: OK, we're half done now. I'll use my original numbers again:... Pin
Luc Pattyn20-Apr-11 4:09
sitebuilderLuc Pattyn20-Apr-11 4:09 
GeneralYour code needs some cleaning up. Here are some comments: 1.... Pin
Luc Pattyn19-Apr-11 13:24
sitebuilderLuc Pattyn19-Apr-11 13:24 
GeneralRe: I've Edited my tips in which I focus on -Standard C# naming... Pin
RaviRanjanKr20-Apr-11 4:00
professionalRaviRanjanKr20-Apr-11 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.