Convert text to image Vb.NET and C#
This article explains how to convert some text into image using Graphics namespace of Microsoft.Net framework, in Visual Basic.Net.
Step 1. Get ready to convert text into image using VB.Net.....!
Step 2. Add following two namespaces in your VB.Net project.
Imports
System.Drawing
Imports System.Drawing.Imaging
Step 3. Declare some variables to set the properties for your required image.
Dim Text As String = "Shahzad Latif"Dim FontColor As Color = Color.Blue
Dim BackColor As Color = Color.YellowDim FontName As String = "Times New Roman"
Dim FontSize As Integer = 14Dim Height As Integer = 40 Dim Width As Integer = 200
Dim FileName As String = "MyImage"
Step 4. Create a Bitmap object using Height and Width properties defined earlier.
Dim
objBitmap As New Bitmap(Width, Height)
Step 5. Create a Graphics object using this Bitmap object.
Dim
objGraphics As Graphics = Graphics.FromImage(objBitmap)
Step 6. Create Color, Font, and PointF objects.
Dim objColor As Color
Dim objFont As New Font(FontName, FontSize)
'Following PointF object defines where the text will be displayed in the
'specified area of the image
Dim objPoint As New PointF(5.0F, 5.0F)
Step 7. Create two SolidBrush type objects.
Dim objBrushForeColor As New SolidBrush(FontColor)
Dim objBrushBackColor As New SolidBrush(BackColor)
Step 8. Draw rectangle using Graphics object and fill it with BackColor.
objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height)
Step 9. Draw Text string on the specified rectangle using Graphics object.
objGraphics.DrawString(Text, objFont, objBrushForeColor, objPoint)
Step 10. Save the image on your hard drive in the specified format.
'Save your image in JPEG format
objBitmap.Save(Application.StartupPath & FileName & ".JPG", ImageFormat.Jpeg)
C#
using System.Drawing;
using System.Drawing.Imaging;
string FontName = "Courier New";
Color FontColor = Color.Black;
Color BackColor = Color.White;
int FontSize = 14;
int Width = 25;
//fontsize times 1.5 is just high enough
//to encase the text without spacer above or below.
float h = (FontSize * 1.5f);
int Height = int.Parse(h.ToString());
//file to save as
string FileName = "MyImage";
//fore color
SolidBrush objBrushForeColor = new SolidBrush(FontColor);
//back color
SolidBrush objBrushBackColor = new SolidBrush(BackColor);
//the point to start the text. I chose horizontal value of zero
//vertical starts at 2 pixels down.
Point objPoint = new Point(0,2);
//font object
Font objFont = new Font(FontName, FontSize);
//bitmap object
Bitmap objBitmap = new Bitmap(Width, Height);
//graphics object
Graphics objGraphics =
System.Drawing.Graphics.FromImage(objBitmap);
//the following line is not needed, but is shown
//in the vb example.. dont know why.
//Color objColor;
//draw a white rectangle
objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width,
(FontSize*1.5f));
//draw the text
objGraphics.DrawString(character.ToString(), objFont, objBrushForeColor,
objPoint);
//save the bitmap.
objBitmap.Save(FileName + ".bmp", ImageFormat.Bmp);