Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Convert text to image in Visual Basic.Net using Graphics namespace, in 10 easy steps.

Author: Shahzad Latif
Download Source Code : 1398_RetrieveImagesFromAccess.zip

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.Yellow

Dim FontName As String = "Times New Roman"

Dim FontSize As Integer = 14

Dim 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)

 

 

Article Comments
Great work done. Keep it up!!!

Posted on 9/19/2006 8:13:26 AM by Arindam

awesome code.. here is a *slightly* modified version I wrote in 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);

Posted on 1/8/2007 6:07:11 PM by Rogelio

Cooooooool . I didnt really know the System.Drawing and the System.Drawing.Imaging namespaces were so very powerful and simple to use !

Posted on 1/23/2007 2:57:28 AM by Junaid

Hi Shahzad,

I must agree that your article has breathed a new life in my project as I was about to get frustated on how to draw text in an image file(jpeg/bmp).

I am a CraadleBaby in field of dotnet programming as I just finished my education and started working on .net platform.

Posted on 3/14/2007 1:28:38 AM by Swapnil Shejul

this article is nice. I want an article about How to convert word file to an image

Posted on 3/16/2007 7:02:01 AM by c venkata chary

dosent work!!!!!! last three lines give errors

Posted on 5/25/2007 10:56:11 AM by

Last Three lines wont work:D what can i do:( im using visual studio 2005:( plz help me:(

Posted on 5/25/2007 10:58:28 AM by Wireless Media Center

worked great for me on c# 2005, thanks a lot I needed it and didn't know its this easy to implement such important feature :)

Posted on 7/14/2007 1:09:40 PM by waleed

i had got a useful information from this.
it is very simple and nice.
i do this as my project with cryptography technic. may you help me?

Posted on 7/18/2007 7:24:47 AM by pandian

Thank you for very nice article.

Posted on 2/22/2008 6:03:23 AM by Yui

how to storing image in text file

Posted on 5/4/2008 3:13:49 AM by abbas Taghizadeh

Works great!, Thank's..
But doesn't works with custom fonts.
Does anyone have solution for that?
I wanted to know how can I place a font file in the app root and draw the image from it .
may you help me?

Posted on 5/4/2008 3:33:53 AM by Joel

This is f-a-n-t-a-s-t-i-c!

Posted on 6/20/2008 10:27:01 AM by netdaniels

dear all
please help me
send me code file to convert string to image format , using vb.net


best regards
yousef

Posted on 6/28/2008 8:13:06 AM by yousef

Nice! But i have problem to convert string to fontstyle! Some one help me?

Posted on 12/1/2008 1:14:45 AM by Atomic

Hello
I am a beginner in C#
I have to convert a text into image/Graph as in..
if I write

“NodeA NodeB
“NodeB NodeC
“NodeA NodeC
“NodeC NodeD

then i shuld get an output in the form of a

circle(NodeA)——>circle(NodeB)..and further like a graph

Hope you get it..i want to create a graph from data and display that graph on screen(window).plz help how should i do it????

Posted on 11/25/2009 6:41:32 AM by Anuradha

nice article.
thanks

Posted on 2/11/2010 8:01:35 AM by mr. romon

gud work boss

Posted on 2/22/2010 3:15:46 AM by arun

nice tutorial,

Posted on 5/9/2010 11:57:29 PM by Benny Kapri

how to convert both text and image together into an image. To clarify my question I can say if I have a text editor where I can add both text,image,audio or video player, how can I convert the whole texteditor.text (it gives me html code) to an image and save it in a file.

Posted on 9/8/2010 4:34:08 AM by Sanhita

I got much help from this. Thanks a lot.

Posted on 11/23/2010 11:42:17 AM by yisen

hi
i'm want your help,
i want the codes of convert image to cartoon in visual basic.
or visual basic .net
thanks

Posted on 11/27/2010 1:15:50 AM by adam

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating controls dynamically on windows form using visual basic.net

Disclaimer - Privacy
© 2002-2017 DevASP.net