Click here to Skip to main content
Click here to Skip to main content

Convert Text to Image

By , 17 May 2011
 
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()
        {
            InitializeComponent();
        }
 
        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);
            font.Dispose();            
            graphics.Flush();
            graphics.Dispose();
            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);
                graphics.Flush();
                font.Dispose();
                graphics.Dispose();
                
 
            }
            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.

License

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

About the Author

RaviRanjankr
Software Developer
India India
Member
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questiondifferent iamge file formatsmemberrandom413 Jan '13 - 22:07 
GeneralRe: But still 3 points(by Luc) pending. Read his 3rd point again...mvpthatraja20 Apr '11 - 5:40 
GeneralI will use this answer in Q & A for all those asking for "Co...memberTarun.K.S21 Apr '11 - 0:21 
GeneralRe: Thanks Tarun :)mvpRaviRanjankr21 Apr '11 - 2:10 
GeneralGood one. My 5memberToniyo Jackson20 Apr '11 - 21:12 
GeneralRe: Thanks Toniyo :)mvpRaviRanjankr20 Apr '11 - 22:06 
GeneralNice. +5ed :)mvpKunal_Chowdhury20 Apr '11 - 8:00 
GeneralRe: Thanks!mvpRaviRanjankr20 Apr '11 - 8:47 
GeneralI second thatraja. And I also think the Font should be a par...memberNiklas Lindquist20 Apr '11 - 1:07 
GeneralI second Luc.mvpthatraja19 Apr '11 - 15:29 
GeneralRe: Now Edited Take a look :)mvpRaviRanjankr20 Apr '11 - 3:08 
GeneralRe: OK, we're half done now. I'll use my original numbers again:...mvpLuc Pattyn20 Apr '11 - 3:09 
GeneralYour code needs some cleaning up. Here are some comments: 1....mvpLuc Pattyn19 Apr '11 - 12:24 
GeneralRe: I've Edited my tips in which I focus on -Standard C# naming...mvpRaviRanjankr20 Apr '11 - 3:00 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 17 May 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid