In this article I will explain with an example, how to add Watermark text to Images / Photos dynamically on the fly in ASP.Net MVC Razor.
The Image (Photo) to be watermarked will be uploaded and using .Net Graphics Library, the uploaded Image (Photo) will be watermarked in ASP.Net MVC Razor.
View
The following View consists of an HTML FileUpload element and a Submit Button enclosed in a Form element.
The HTML Form has been created using the Html.BeginForm method which accepts the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<span>Select File:</span>
<input type="file" name="postedFile"/>
<hr/>
<input type="submit" value="Upload"/>
}
</div>
</body>
</html>
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
Controller
The Action method Index by default supports the GET operation and hence another overridden method for POST operation is created which accepts the parameter of type HttpPostedFileBase.
Note: The name of the HttpPostedFileBase parameter and the name of HTML FileUpload element must be exact same, otherwise the HttpPostedFileBase parameter will be NULL.
The uploaded Image file is first read into a Bitmap object. Then using the Graphics class the Font, Font Size and the Color of the Watermark Text is set.
Now the Watermark Text is ready to be drawn on the Image, but in order to place it on the right bottom corner we need to determine the height and the width of the Watermark Text.
Using the height and the width of the Watermark Text, the X-Y coordinate is calibrated and the Watermark Text is drawn on the image.
Finally the Bitmap is saved into a MemoryStream object which later downloaded as an Image file.
public class HomeController : Controller
{
// GET: Home
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
if (postedFile != null)
{
string watermarkText = "© ASPSnippets.com";
//Get the file name.
string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName) + ".png";
//Read the File into a Bitmap.
using (Bitmap bmp = new Bitmap(postedFile.InputStream, false))
{
using (Graphics grp = Graphics.FromImage(bmp))
{
//Set the Color of the Watermark text.
Brush brush = new SolidBrush(Color.Red);
//Set the Font and its size.
Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
//Determine the size of the Watermark text.
SizeF textSize = new SizeF();
textSize = grp.MeasureString(watermarkText, font);
//Position the text and draw it on the image.
Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
grp.DrawString(watermarkText, font, brush, position);
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Watermarked image to the MemoryStream.
bmp.Save(memoryStream, ImageFormat.Png);
memoryStream.Position = 0;
return File(memoryStream.ToArray(), "image/png", fileName);
}
}
}
}
return View();
}
}
Screenshot
Downloads