Write Text on image in C# Asp.net

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WriteTextonImage.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:FileUpload ID="Fileupload1" runat="server" />

            <asp:Button ID="button1" runat="server" Text="Upload" OnClick="button1_Click" />

        </div>

    </form>

</body>

</html>

-------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

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

        }

        protected void button1_Click(object sender, EventArgs e)
        {
            string WaterMarkTextHeader = "Authorised Signatory";

            string filename = Path.GetFileNameWithoutExtension(Fileupload1.PostedFile.FileName) + ".png";

           // Read file into Bitmap
            using (Bitmap bmp = new Bitmap(Fileupload1.PostedFile.InputStream, false))
            {
                using (Graphics grp = Graphics.FromImage(bmp))
                {
                    //Set the color of the watermark Text.
                    Brush brush = new SolidBrush(Color.Black);

                    // Set the font and its size
                    Font font = new System.Drawing.Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);

                    //Determine the size of the watermark text
                    SizeF textSize = new SizeF();
                    textSize = grp.MeasureString(WaterMarkTextHeader, font);

                    //Position the text and draw it on the image
                    Point position = new Point((bmp.Width - ((int)textSize.Width + 50)), (bmp.Height - ((int)textSize.Height + 0)));

                    grp.DrawString(WaterMarkTextHeader, font, brush, position);

                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        //Save the waterMark image to MemoryStream
                        bmp.Save(memoryStream, ImageFormat.Png);
                        memoryStream.Position = 0;

                        //start File download
                        Response.Clear();
                        Response.ContentType = "image/png";
                        Response.AddHeader("Context-Disposition", "attachment; filename = '"+filename+"'") ;

                        // Write the MemoryStream to the Response
                        memoryStream.WriteTo(Response.OutputStream);
                        Response.Flush();
                        Response.Close();
                        Response.End();
                    }
                }
            }

        }
    }
}
-------------------------------------------------------------------------------------------------------------------------







0 Comments