Posts Tagged ‘Text to Image’

Convert Text to Image using c#

We can Convert text to image in c# using System.Drawing namespace.
Here we create a bitmap image and use its Graphics property to convert text passed to image and place the image in a picture box.

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

Download

Popularity: 13% [?]

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars