ASP net Write Text On Image C#.net and Vb.net program

ASP net Write Text On Image C#.net and Vb.net program

Asp.net Write Text On Image or Picture. In Asp.net, you can write text on Image using Graphics library. Here we will see very simple web application code which accepts string from user and write it on Image. The original image remains unchanged. This is how you can Tag the people or friends on image. I’m writing my full name on my Head Shot Picture.

After clicking on write button, image will be available for download. You can write the code to open Save close file download options.

You can also tell users to upload the image and ask to tagging. How to upload image in Asp.net and save it on SQL server. Even you can store the image on File Server.

C#.net Namespace.

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

Vb.net Namespace.

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging

.aspx Code.

<form id="form1" runat="server">
    <div>
        Text To Write On Image:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="Button1"
            runat="server" OnClick="Button1_Click" Text="Write" /></div>
    </form>

Asp.net Write Text On Image C#.net.

protected void Button1_Click(object sender, EventArgs e)
  {
    string path = Server.MapPath(@"uploads\satalaj.jpg");
    
    Bitmap b = new Bitmap(path);
    Graphics g = Graphics.FromImage(b);
    
    g.SmoothingMode = SmoothingMode.AntiAlias;
    
    string inputString = TextBox1.Text;
    Font f = new Font("Arial",20,FontStyle.Bold);
       
    
    g.DrawString(inputString,f, SystemBrushes.WindowText, new Point(0,150));
    
    Response.Clear();
    Response.ContentType = "image/jpeg";
    
    b.Save(Response.OutputStream, ImageFormat.Jpeg);   
    
  }

Description:

Server.MapPath method is used to get physical path of satalaj.jpg image file. We load found image in an instance of Bitmap. The instance is then passed to Graphics object constructor. Object of Graphics writes text on image to render. Save method of Bitmap instance writes updated image to response stream.

string path = Server.MapPath(@"uploads\satalaj.jpg");
    
    Bitmap b = new Bitmap(path);
    Graphics g = Graphics.FromImage(b);

DrawString method of Graphics instance writes text to image at specified location.

g.DrawString(inputString,f, SystemBrushes.WindowText, new Point(0,150));

VB.net Write Text on Image.

Public Partial Class WriteText_On_Image_Demo
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)

	End Sub
	Protected Sub Button1_Click(sender As Object, e As EventArgs)
		Dim path As String = Server.MapPath("uploads\satalaj.jpg")

		Dim b As New Bitmap(path)
		Dim g As Graphics = Graphics.FromImage(b)

		g.SmoothingMode = SmoothingMode.AntiAlias

		Dim inputString As String = TextBox1.Text
		Dim f As New Font("Arial", 20, FontStyle.Bold)


		g.DrawString(inputString, f, SystemBrushes.WindowText, New Point(0, 150))

		Response.Clear()
		Response.ContentType = "image/jpeg"

		b.Save(Response.OutputStream, ImageFormat.Jpeg)

	End Sub
End Class

Summery We saw how to write text on image or picture. You can extend the functionality of this code to do image tagging like features provided in major social networking websites (FaceBook, Google Plus and Orkut).

Tags: