C# .NET - how to convert text to image

Asked By chinna samy on 04-Nov-08 01:52 AM
how to convert text to image

use this 10 steps

Web Star replied to chinna samy on 04-Nov-08 01:58 AM

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)

reply

Binny ch replied to chinna samy on 04-Nov-08 02:02 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 System.Drawing;

using System.Drawing.Imaging;

 

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

 

1.           string Text = "Shahzad Latif";

2.          

3.           Color FontColor = Color.Blue;

4.          

5.           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.           Bitmap objBitmap = new Bitmap(Width, Height);  

 

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

 

 

1.           Graphics objGraphics = Graphics.FromImage(objBitmap);

 

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

 

 

1.           Color objColor;

2.          

3.           Font objFont = new Font(FontName, FontSize);

4.          

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

  //the specified area of the image

6.          

7.           PointF objPoint = new PointF(5f, 5f);

 

 

Step 7. Create two SolidBrush type objects.

 

 

1.           SolidBrush objBrushForeColor = new SolidBrush(FontColor);

2.          

3.           SolidBrush objBrushBackColor = new 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);

reply

Binny ch replied to chinna samy on 04-Nov-08 02:03 AM
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);
how to convert text to image in C#
Jignesh Shah replied to chinna samy on 04-Nov-08 02:04 AM

I think you are asking in C# .NET and not VB .NET.

You can use C#  GDI+ library to convert text to image. Use below code:

private Bitmap CreateBitmapImage(string sImageText)
    {
        Bitmap objBmpImage = new Bitmap(1, 1);
        int intWidth = 0;
        int intHeight = 0;
        // Create the Font object for the image text drawing.
        Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
       // Create a graphics object to measure the text's width and height.
       Graphics objGraphics = Graphics.FromImage(objBmpImage);
       // This is where the bitmap size is determined.
       intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
       intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
       // Create the bmpImage again with the correct size for the text and font.
       objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
       // Add the colors to the new bitmap.
       objGraphics = Graphics.FromImage(objBmpImage);
       // Set Background color
       objGraphics.Clear(Color.White);
       objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
       objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
       objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
       objGraphics.Flush();
       return (objBmpImage);
  }
text to JPEG :
Jignesh Shah replied to chinna samy on 04-Nov-08 02:05 AM

See http://www.eggheadcafe.com/community/aspnet/2/10036257/convert-text-to-image-c.aspx for this.

 

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

Convert Text to Image using C#
Shailendrasinh Parmar replied to chinna samy on 04-Nov-08 02:52 AM

See the following article for converting text into Image using C#

http://chiragrdarji.wordpress.com/2008/05/09/generate-image-from-text-using-c-or-convert-text-in-to-image-using-c/

Hope it helps.

Solution
san san replied to chinna samy on 04-Nov-08 09:29 AM

Hi

Take a look at the bellow code...

protected void Page_Load(object sender, EventArgs e)

{

Response.Clear();

Bitmap TmpImage = WriteText("C:\MyImage.JPG", "Sample Text", 20, 20);

TmpImage.Save("C:\MyImage.jpeg", ImageFormat.Jpeg);

Response.End();

}

WriteText method

Bitmap WriteText(String input, String text, Int32 x, Int32 y)

{

Bitmap TmpBitmap = new Bitmap(input);

Graphics TmpGraphics = Graphics.FromImage(TmpBitmap);

TmpGraphics.DrawString(text, new Font("Arial", 63.0f, GraphicsUnit.Pixel), new SolidBrush(Color.Red), x, y);

return TmpBitmap;

}

convert text to image
C_A P replied to chinna samy on 05-Nov-08 12:11 AM
   1: private Bitmap CreateBitmapImage(string sImageText)
   2: {
   3:     Bitmap objBmpImage = new Bitmap(1, 1);
   4:  
   5:     int intWidth = 0;
   6:     int intHeight = 0;
   7:  
   8:     // Create the Font object for the image text drawing.
   9:     Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
  10:  
  11:     // Create a graphics object to measure the text's width and height.
  12:     Graphics objGraphics = Graphics.FromImage(objBmpImage);
  13:     
  14:     // This is where the bitmap size is determined.
  15:     intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
  16:     intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
  17:  
  18:     // Create the bmpImage again with the correct size for the text and font.
  19:     objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
  20:  
  21:     // Add the colors to the new bitmap.
  22:     objGraphics = Graphics.FromImage(objBmpImage);
  23:  
  24:     // Set Background color
  25:     objGraphics.Clear(Color.White);
  26:     objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
  27:     objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
  28:     objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
  29:     objGraphics.Flush();
  30:  
  31:     return (objBmpImage);
  32: }
try this code to convert text to image
C_A P replied to chinna samy on 05-Nov-08 12:12 AM

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);
Manoj Balure replied to Web Star on 29-May-12 02:28 AM
Hello,
      I have used source code that u given.that helped me lot.
Thank u...

Regards,
Manoj