Tuesday, 18 March 2014

Adding Water Mark on Pictures Asp.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Drawing.Text;


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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Execute below code at the event, where u wants Water Marking.

           // Create a bitmap object of the Image, Here I am taking image from File Control "FileTest"
            Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream);
            Graphics canvas = Graphics.FromImage(bmp);
            try
            {
                Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
                canvas = Graphics.FromImage(bmpNew);
                canvas.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
                bmp = bmpNew;
            }
            catch(Exception ee) // Catch exceptions
            {
                Response.Write(ee.Message);
            }
            // Here replace "Text" with your text and you also can assign Font Family, Color, Position Of Text etc.
            canvas.DrawString("GN somro", new Font("Verdana", 14, FontStyle.Bold), new SolidBrush(Color.Beige), (bmp.Width / 2), (bmp.Height / 2));
            // Save or display the image where you want.
            bmp.Save(System.Web.HttpContext.Current.Server.MapPath("~/images/") + FileUpload1.PostedFile.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);

        }
    }
}

No comments:

Post a Comment