Water mark on image with asp.net

In this article I'll show you making of water mark on image with asp.net. Make two folders on server tmp and images. In default.aspx take three controls Label, Fileupload and Button as given below:

Now double click on btnSave, default.aspx.cs page will open with btn click event.

protected void btnSave_Click(object sender, EventArgs e)
{
}



In default.aspx.cs add following namespaces with other given namespaces.
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

//and now make a method for water mark

protected void WaterMark(string FileName)
{
string path =Server.MapPath( "~/tmp/"+FileName);
string watermark = "abc.com";
Bitmap objBmp;
objBmp = new Bitmap(path);
Graphics objGraphics;
try
{
objGraphics = Graphics.FromImage(objBmp);
}
catch
{
objBmp = new Bitmap(objBmp.Width, objBmp.Height);
objGraphics = Graphics.FromImage(objBmp);
objGraphics.DrawImage(objBmp, new Rectangle(0, 0, objBmp.Width, objBmp.Height), 0, 0, objBmp.Width, objBmp.Height, GraphicsUnit.Pixel);
}
int size = (objBmp.Width / watermark.Length);
System.Drawing.StringFormat Format = new System.Drawing.StringFormat(StringFormatFlags.NoWrap);
objGraphics.DrawString(watermark, new Font("Arial", size, FontStyle.Bold), new SolidBrush(Color.FromArgb(60, 255, 255, 255)), 0, 0 Format);
objBmp.Save(Server.MapPath( "~/images/"+FileName));
}


Now write write coding on btnSave click event
protected void btnSave_Click(object sender, EventArgs e)
{

string extension=Path.GetExtension(fileUpload.FileName);

switch (extension.ToLower())
{case ".jpg":
case ".jpeg":
case ".png":
case ".gif":
fileUpload.SaveAs(Server.MapPath("~/tmp/"+fileUpload.FileName));
WaterMark(fileUpload.FileName);
lbl.Text = "File is saved with water mark.";
break;
default:
lbl.Text = "Given file is not an image file";
break;
}
}
Now try it.


0 komentar:

Posting Komentar