Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

Generate Images of your Text

Viewed:  1484 
Posted On:  01/11/2014 21:51:52 
How we can generate image of our text in c# ? 

In this article I am going to show how we can generate image of any text.

Below is my aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <title>Generate Image of Text</title>
    <form id="form1" runat="server">
        <table style="width: 90%; background-color: #F5F5F5; border: solid 10px Green;" align="center"
            cellpadding="10" cellspacing="10">
                <td height="10px" style="background-color: Yellow; font-weight: bold; font-family: Georgia;
                    font-size: 18pt; color: Red;" align="center">
                    Generate Image of Your Text
                    Enter Text #:
                    <asp:TextBox runat="server" ID="txtYourText" Width="300px"></asp:TextBox>
                    <asp:Button ID="btnConvert" runat="server" Text="Generate Image" OnClick=" btnGenerateImage _Click" />

<asp:Image ID="imgText" runat="server" Visible="false" />

Below is my aspx.cs code:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void btnGenerateImage_Click(object sender, EventArgs e)
        string text = txtYourText.Text.Trim();
        Bitmap bitmap = new Bitmap(1, 1);
        Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel);
        Graphics graphics = Graphics.FromImage(bitmap);
        int width = (int)graphics.MeasureString(text, font).Width;
        int height = (int)graphics.MeasureString(text, font).Height;
        bitmap = new Bitmap(bitmap, new Size(width, height));
        graphics = Graphics.FromImage(bitmap);
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
        graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
        Random random = new Random();
        string fileName = Convert.ToString(random.Next(1, 2000)) + ".jpg";
        bitmap.Save(Server.MapPath("~/MyTextImages/") + fileName, ImageFormat.Jpeg);
        imgText.ImageUrl = "~/MyTextImages/" + fileName;
        imgText.Visible = true;

Now run the application:


Image 1.


Image 2.

Now you can view your all images..


Image 3.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us