Creating Watermark to Protect Images in Asp.Net

Sometimes, there is requirement to protect our images that we have used in our website. There are many ways to protect images but one of the best is to use watermark. Now, watermark solution can be handled in many ways. It can be handled in client side as well as server side. First i will show you the client side solution then i will show the server side solution which we will do it in Asp.Net MVC.

For client side, there is a jQuery plugin watermark.js available which you can download for free and use it. Basically  you give an class to your images and then pass it to this plugin then this jQuery plugin can alter your images and merge your assigned watermark (as an image) to the original image and convert it then to base64 encoded string. First make sure to reference the plugin in your document and by the following code, you can supply then a watermark;

    <script>
        var load = false;
        window.onload = function () {
            if (!load) {
                wmark.init({
                    "position": "top-left", // "top-left"|"top-right"|"bottom-right"|"bottom-left" default "bottom-right"
                    "opacity": 20, // default 50
                    "className": "watermark", // default "watermark"
                    "path": "/Themes/DefaultClean/Content/images/watermark.png"
                });

                load = true;
            }
        }
	</script>
But this solution is a hack, because the original images are still without a watermark. However it works for normal users. That's why i will show you another way which is to watermark your images on server side using c# in asp.net.
For Asp.Net, i will be using nopCommerce which is open source asp.net mvc ecommerce solution. But the code will work for any asp.net application. So, i will write this code in nopCommerce Solution -> Presentation -> Admin -> PictureController - > AsyncUpload Action Result.

                Bitmap outputImage = null;
                Graphics g = null;
                try
                {
                    // the final image
                    var mStream = new MemoryStream();
                    mStream.Write(fileBinary, 0, fileBinary.Length); //fileBinary is a byte[] array of a file i.e image
                    mStream.Seek(0, SeekOrigin.Begin);

                    var image = Image.FromStream(mStream);
                    outputImage = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

                    g = Graphics.FromImage(outputImage);
                    g.CompositingMode = CompositingMode.SourceCopy;
                    Rectangle destRect = new Rectangle(0, 0, image.Width, image.Height);

                    // the photo
                    using (var BasicPhoto = new Bitmap(image))
                    {
                        g.DrawImage(BasicPhoto, destRect, 0, 0, BasicPhoto.Width, BasicPhoto.Height, GraphicsUnit.Pixel);
                    }

                    g.CompositingMode = CompositingMode.SourceOver;
                    string cWaterMarkPhotoOnDisk = "~/Themes/DefaultClean/Content/images/watermark.png";
                    // the watermark
                    using (var WaterMark = new Bitmap(System.Web.HttpContext.Current.Server.MapPath(cWaterMarkPhotoOnDisk)))
                    {
                        Rectangle destWaterRect = new Rectangle(image.Width / 2 - WaterMark.Width / 2, image.Height / 2 - WaterMark.Height / 2, WaterMark.Width, WaterMark.Height);
                        g.DrawImage(WaterMark, destWaterRect, 0, 0, WaterMark.Width, WaterMark.Height, GraphicsUnit.Pixel);
                    }
                    var tempStream = new MemoryStream();
                    outputImage.Save(tempStream, ImageFormat.Jpeg);
                    fileBinary = tempStream.ToArray();
                    mStream.Dispose();
                    tempStream.Dispose();
                }
                catch (Exception x)
                {
                    return Json(new { success = false, error = x.Message.ToString() }, "text/plain");
                }
                finally
                {
                    if (outputImage != null)
                        outputImage.Dispose();

                    if (g != null)
                        g.Dispose();
                }
From the code comments, you can see that first i am converting the image into a bitmap and then i created a rectangle. In this rectangle i am pulling the watermark image from the server directory and then draw this rectangle on the orignal image. Now the point here is i want to draw this water mark image on the center of the orignal image and the code for that is;

Rectangle destWaterRect = new Rectangle(image.Width / 2 - WaterMark.Width / 2, image.Height / 2 - WaterMark.Height / 2, WaterMark.Width, WaterMark.Height);
g.DrawImage(WaterMark, destWaterRect, 0, 0, WaterMark.Width, WaterMark.Height, GraphicsUnit.Pixel);
Inshort, i am taking the orignal widh of the image and divide it by two and the the watermark image and divide by and then minus it from the original image width. Now, if we test this, we should see a watermark upon image.
You can move on even make your own Ui for providing the width and height from a jQuery plugin. But i skip that as mostly it did work for my need.



Write your own review
Bad Excellent