Convert Text to Image using C#
Many times we need to convert a textual information to image, here I explained how to convert textual information from textbox or richtextbox to image using C#
Convert Text to Image using C#
Introduction
This is the basic article, you can called it as a technical tip in which we will convert a text to image, The Text is collected from any input control like, Textbox, RichTextbox etc.
with the help of 'System.Drawing.Imaging' we can achieve this taskStart with it
We need following things to accomplish the goal
1. Windows form
2. Button
3. Input Control (like textbox, RichTextBox etc)
Here i used RichTextBox
first define all properties that we need to set to an image
//put following properties on form
public Color FontColor { get; set; }
public Color FontBackColor { get; set; }
public string FontName { get; set; }
public string ImagePath { get; set; }
public int FontSize { get; set; }
public int ImageHeight { get; set; }
public int ImageWidth { get; set; }
public Bitmap ImageText { get; set; }
public Graphics ImageGraphics { get; set; }
public Font ImageFont { get; set; }
public SolidBrush BrushForeColor { get; set; }
public SolidBrush BrushBackColor { get; set; }
public PointF ImagePointF { get; set; }
then we need to get all the properties of Richtextbox and use them to draw an image
see below snippet
//import a namespace on top of the form
using System.IO;
using System.Drawing.Imaging;
//write a function that will collect all properties of input control and create an image
private void DrawText(string text)
{
richTextBox1.SelectAll();
FontColor = richTextBox1.SelectionColor;
FontBackColor = richTextBox1.SelectionBackColor;
FontName = "Arial";// richTextBox1.SelectionFont.Name;
FontSize = 20; //Convert.ToInt16(richTextBox1.SelectionFont.Size);
ImageHeight = richTextBox1.Height;
ImageWidth = richTextBox1.Width;
ImagePath = @"D:\Test.JPEG"; //give the file name you want to export to as image
ImageText = new Bitmap(ImageWidth, ImageHeight);
ImageGraphics = Graphics.FromImage(ImageText);
ImageFont = new Font(FontName, FontSize);
ImagePointF = new PointF(5, 5);
BrushForeColor = new SolidBrush(FontColor);
BrushBackColor = new SolidBrush(FontBackColor);
ImageGraphics.FillRectangle(BrushBackColor, 0, 0, ImageWidth, ImageHeight);
ImageGraphics.DrawString(text, ImageFont, BrushForeColor, ImagePointF);
//Draw a byte and create image file
string outputFileName = ImagePath;
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
ImageText.Save(memory, ImageFormat.Jpeg);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
}
//call above function on button click and pass textbox text to function
private void cmdMakeTextToImage_Click(object sender, EventArgs e)
{
DrawText(richTextBox1.Text);
}
here is input and output snaps
INPUT
OUTPUT
Note : I have attached demo code with this article, download and check it.
Link to download code : Demo Source code
Summing up
With the help of 'Graphics' class we can create an image from textbox/Richtextbox
Suggestion and Queries always welcome
Thanks
Koolprasad2003