Add watermark to images in asp.net
Want to create an application/website where you users can add watermarks to their images? I have here demonstrated an Asp.Net code snippet for adding watermark to images.
Adding watermark to images is useful when you need to protect your copyrights or if you want to add comments to your photos. Most of us, who run websites/blogs, face issues of our images being copied and displayed on other blogs or sites. One of the solutions to this problem is watermarking our images with our logo or watermarking the website name on the images. This article describes how to add watermark to images.
Uploading your photos on the web can become safer if you are allowed to add a custom watermark to your photos. Asp.Net code for adding watermark to your image.
//Namespace used
using System.Drawing;
//Execute below code at the event, where you want Water Marking
// Create a bitmap object of the Image, Here I am taking image from File Control "FileUploadImage"
Bitmap original_image = new Bitmap(FileUploadImage.FileContent);
string sWaterMark = "Myfoto";//String to be watermarked
int fontsize = ((original_image.Width * 2) / (sWaterMark.Length * 3));
int x = original_image.Width / 2;
int y = original_image.Height * 9 / 20;
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.FormatFlags = StringFormatFlags.NoWrap;
//drawing string on Image
Graphics graphic = Graphics.FromImage(original_image);
graphic.DrawString(sWaterMark, new Font("Verdana", fontsize, FontStyle.Bold), new SolidBrush(Color.FromArgb(80, 255, 255, 255)), x, y, drawFormat);
original_image.Save(MapPath("~/Uploads/Images/" + FileUploadImage.PostedFile.FileName));
if (original_image != null) original_image.Dispose();
if (graphic != null) graphic.Dispose();