.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Convert text to image

Posted By:satyapriyanayak       Posted Date: September 30, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

We will know how to convert the text to image. Here we have two controls button and textbox. When user enters any text to the textbox and clicks the button, then the text is converted to its corresponding image.

Note: - Here we need two namespaces.
using System.Drawing;
using System.Drawing.Imaging;

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
     <asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
        <br /><br />
        <asp:Button ID="btnconvert" runat="server" Text="Convert to image"
            onclick="btnconvert_Click" />

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.IO;
using System.Drawing;
using System.Drawing.Imaging;
public partial class _Default : System.Web.UI.Page
    protected void btnconvert_Click(object sender, EventArgs e)
        Bitmap bit1 = new Bitmap(200, 30, System.Drawing.Imaging.PixelFormat.Format64bppArgb);
        Graphics gph1 = Graphics.FromImage(bit1);
        string text;
        text = TextBox1.Text;
        gph1.DrawString(text, new Font("Arial", 12, FontStyle.Bold),
        new SolidBrush(Color.Red), new PointF(0.4F, 2.4F));
        Response.ContentType = "image/Jpeg";
        bit1.Save(Response.OutputStream, ImageFormat.Jpeg);


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend