Sunday, 21 January 2018

Create (Add) Watermark Text to Images (Photo) in ASP.Net MVC

Create (Add) Watermark Text to Images (Photo) in ASP.Net MVC

Description:

In this example we explain that how to create watermark text to images in Asp.Net MVC.or how to create Watermark Text to photo in MVC razor. Or how to impalement Watermark Text in Images same like as PDF in MVC.

We already explain that how to create watermark Text in PDF in asp.net but here we demonstrate that how to implement or create watermark text in images or photo using MVC razor.
Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase postedFile)
        {
            if (postedFile != null)
            {
                string watermarkText = "here you can set your Watermark Text";

                //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();
        }
    }

View: 

<html>
<head>
    <title>Create (Add) Watermark Text to Images (Photo) in ASP.Net MVC</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "Employee", 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>


This entry was posted in :

0 comments:

Post a Comment