Saturday, 3 August 2013

Writing Text On Image

Aspx Page
-----------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WritingOnImageTest.aspx.cs" Inherits="WritingOnImageTest" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div style="vertical-align:top">
        Image:<asp:FileUpload ID="FUImage" runat="server" />
        <br />
         To:<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
         <br />
         Enter Text:<asp:TextBox ID="txtText" runat="server"  TextMode="MultiLine" Rows="5" Columns="10"></asp:TextBox>
         <br />
         From:<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>



.Cs Page
---------
using System;
using System.Collections;
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;
using System.Net.Mail;
using System.Net.Mime;

public partial class WritingOnImageTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Save()
    {

        string image = System.IO.Path.GetFullPath(FUImage.PostedFile.FileName);
    System.Drawing.Bitmap originalImage = new System.Drawing.Bitmap(image);
    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(originalImage);
    PointF pointToWriteAt = new PointF(10, 10);

    PointF pointToWriteAt_1 = new PointF(100,150);
    PointF pointToWriteAt_2 = new PointF(560, 360);

    string textTo = this.txtTo.Text.Trim();
    string textFrom =  this.txtFrom.Text.Trim();
    string textToWrite = this.txtText.Text.Trim();
    Font font = new Font("Arial", 30);

    graphics.DrawString(textTo, font, Brushes.Red, pointToWriteAt);
    graphics.DrawString(textToWrite, font, Brushes.Red, pointToWriteAt_1);
    graphics.DrawString(textFrom, font, Brushes.Red, pointToWriteAt_2);

   
    string SaveImage =System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg" ;
    originalImage.Save(Server.MapPath("~\\SaveImages\\" + SaveImage));

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Save();
    }
}