C# .NET - Convert Text to Image

Asked By Bhuiyan Morshed on 14-May-08 05:13 PM
Hello,
I want to convert a text to image using c#. Is any sample code or hints or links ?

I look for your response

Regards
Morshed

Convert Text to Image

Partha Mandayam replied to Bhuiyan Morshed on 14-May-08 05:36 PM
Check out this article http://www.devasp.net/net/articles/display/139.html

Convert Text to Image

Bhuiyan Morshed replied to Partha Mandayam on 14-May-08 05:42 PM
Hi Partha Mandayam,
Thank you for quick reply.

Regards
Morshed

Convert Text to image C# Code

Rakesh Vikram replied to Bhuiyan Morshed on 15-May-08 12:26 AM

Step 1. Get ready to convert text into image using C#.Net.....!

Step 2. Add following two namespaces in your C#.Net project.

using http://labs.developerfusion.co.uk/SourceViewer/view/SSCLI/System.Drawing/;

using http://labs.developerfusion.co.uk/SourceViewer/view/SSCLI/System.Drawing.Imaging/;

 

Step 3. Declare some variables to set the properties for your required image.

 

1.           string Text = "Shahzad Latif";

2.          

3.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Color/ FontColor = Color.Blue;

4.          

5.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Color/ BackColor = Color.Yellow;

6.          

7.           string FontName = "Times New Roman";

8.          

9.           int FontSize = 14;

10.      

11.       int Height = 40;

12.      

13.       int Width = 200;

14.      

15.       string FileName = "MyImage";

 

 

Step 4. Create a Bitmap object using Height and Width properties defined earlier.

 

1.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Bitmap/ objBitmap = new http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Bitmap/(Width, Height);  

 

Step 5. Create a Graphics object using this Bitmap object.

 

 

1.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Graphics/ objGraphics = Graphics.FromImage(objBitmap);

 

Step 6. Create Color, Font, and  PointF objects.

 

 

1.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Color/ objColor;

2.          

3.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Font/ objFont = new http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/Font/(FontName, FontSize);

4.          

5.           //Following PointF object defines where the text will be displayed in       

  //the specified area of the image

6.          

7.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/PointF/ objPoint = new http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/PointF/(5f, 5f);

 

 

Step 7. Create two SolidBrush type objects.

 

 

1.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/SolidBrush/ objBrushForeColor = new http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/SolidBrush/(FontColor);

2.          

3.           http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/SolidBrush/ objBrushBackColor = new http://labs.developerfusion.co.uk/SourceViewer/search/SSCLI/SolidBrush/(BackColor);

 

 

 

 

 

Step 8. Draw rectangle using Graphics object and fill it with BackColor.

 

1.       objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height);

 

 

Step 9. Draw Text string on the specified rectangle using Graphics object.

 

1.       objGraphics.DrawString(Text, objFont, objBrushForeColor, objPoint);

 

 

Step 10. Save the image on your hard drive in the specified format.

 

1.       //Save your image in JPEG format

2.       objBitmap.Save(Application.StartupPath + FileName + ".JPG", ImageFormat.Jpeg);

 

See This
Shailendrasinh Parmar replied to Bhuiyan Morshed on 15-May-08 02:17 AM

Convert Text to Image 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);

Example code:
Chirag Bhavsar replied to Bhuiyan Morshed on 16-May-08 02:40 AM

The following code example is designed for use with Windows Forms, and it requires http://msdn.microsoft.com/en-us/library/system.windows.forms.painteventargs.aspxe, which is a parameter of the http://msdn.microsoft.com/en-us/library/system.windows.forms.control.paint.aspx event handler. The code performs the following actions:

·         Creates a text string to draw.

·         Defines the font as Arial (16pt).

·         Creates a solid, black brush to draw with.

·         Creates the coordinates of a point for the upper-left corner at which to draw the text.

·         Sets the format of the string to draw vertically

·         Draws the string to the screen using the font, brush, destination point, and format.

VB.NET Example:-

Public Sub DrawStringFloatFormat(ByVal e As PaintEventArgs)

 

    ' Create string to draw.

    Dim drawString As [String] = "Sample Text"

 

    ' Create font and brush.

    Dim drawFont As New Font("Arial", 16)

    Dim drawBrush As New SolidBrush(Color.Black)

 

    ' Create point for upper-left corner of drawing.

    Dim x As Single = 150.0F

    Dim y As Single = 50.0F

 

    ' Set format of string.

    Dim drawFormat As New StringFormat

    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical

 

    ' Draw string to screen.

    e.Graphics.DrawString(drawString, drawFont, drawBrush, _

    x, y, drawFormat)

End Sub

 

C#.NET Example:-

public void DrawStringFloatFormat(PaintEventArgs e)

{

 

    // Create string to draw.

    String drawString = "Sample Text";

 

    // Create font and brush.

    Font drawFont = new Font("Arial", 16);

    SolidBrush drawBrush = new SolidBrush(Color.Black);

 

    // Create point for upper-left corner of drawing.

    float x = 150.0F;

    float y =  50.0F;

 

    // Set format of string.

    StringFormat drawFormat = new StringFormat();

    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;

 

    // Draw string to screen.

    e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);

}

link to refer: http://msdn.microsoft.com/en-us/library/d3thd43b.aspx

MANDEEP replied to Bhuiyan Morshed on 12-May-10 08:22 AM
private void TextToImage()
{
Color BackColor = Color.White;
String FontName = "Times New Roman";
int FontSize = 25;
int Height = 50;
int Width = 300;
<span style="color: rgb(56, 118, 29);">
//creating bitmap image
<span style="color: rgb(0, 0, 0);">Bitmap </span></span>bitmap = new Bitmap(Width, Height);
 
<span style="color: rgb(56, 118, 29);">//FromImage method creates a new Graphics from the specified Image.</span>
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray;
Font font = new Font(FontName, FontSize); 
 
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
 
Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
<span style="color: rgb(56, 118, 29);">//Drawing a rectangle and filling the background as white
<span style="color: rgb(0, 0, 0);">graphics</span></span>.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle); <span style="color: rgb(56, 118, 29);">
 
//giving the font and color to the string passed</span>
graphics.DrawString(txtEnter.Text,font,Brushes.Red, 0, 0);
pictureBox1.Image = bitmap;
 
}