Wednesday, 15 May 2013

WaterMarkImage in Your Project


<h2>
        Welcome To GrayLogic!
    </h2>
    <p>
        please Select a Image File:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:FileUpload ID="FileUpload1" runat="server" />
    </p>
    <p>
        <asp:Button ID="btnUpload" runat="server" Text="ImageUpload"
            onclick="btnUpload_Click" />
    </p>
    <p>
        <asp:Image ID="Image1" runat="server" Width="250px" Height="250px" BorderColor="Black"
            BorderStyle="Solid" BorderWidth="1" />
    </p>



 {
        //Code For WaterMarking
        // Create an Image Object and fill it with the stream of the selected image.
        System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
        // Get the height and width of an image.
        int Height = image.Height;
        int Width = image.Width;
        // Creating the Bitmap Object and assigning the width and height of the image.
        // It internally creates a graphical image with the same dimension.
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Width, Height);
        // Creating a Graphics Object from the Bitmap Object
        System.Drawing.Graphics graphics1 = System.Drawing.Graphics.FromImage((System.Drawing.Image)bmp);
        // Assigning few properties to the Graphics Object, to maintain the quality of the image.
        graphics1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        graphics1.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        graphics1.Clear(System.Drawing.Color.Transparent);
        graphics1.DrawImage(image, 0, 0, Width, Height);
        // Create the Font and the Brush Object and assign appropriate parameters inorder to add watermark to the image.
        System.Drawing.Font font = new System.Drawing.Font("Arial", 20);
        System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Aqua);

        // Drawstring actually writes / watermarks the image with the specified content.
        // Parameters contain :- Watermark Content, font style, brush used, x and y position for the string to be written
        graphics1.DrawString("GrayLogic", font, brush, 25F, 115F);
        // Create the image object from the bitmap and then use it to save the watermarked image in the folder.
        System.Drawing.Image newImage = (System.Drawing.Image)bmp;
        if (FileUpload1.HasFile)
        {
            //Get the Guid
            string myguid = System.Guid.NewGuid().ToString();
            //Remove The Hyphens
            myguid = myguid.Replace("-", string.Empty);
            //Decresing the myguid
            myguid = myguid.Substring(0, myguid.Length-20);
            // Getting the file name of the selected image
            string FileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
            // Getting the file extension of the selected image
            string FileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower().Trim();
            // Checking the format of the Image file.
            if (FileExtension != ".jpg" && FileExtension != ".jpeg" && FileExtension != ".png" && FileExtension != ".gif" && FileExtension != ".bmp")
            {
            string alert="alert('File Format Not Supproted,Only .jpg,.jpeg,.png,.gif,.bmp file Formats are Supported');";
            ScriptManager.RegisterStartupScript(this, GetType(), "JScript", alert, true);
            }
            // Checking if the Width or height is greater than 400px
            else if (image.PhysicalDimension.Width > 600 || image.PhysicalDimension.Height > 600)
            {
                string alert = "alert('Image File should be Exactly 400*400 dimesions.Your Current Image Width is " + image.PhysicalDimension.Width + "and Height is" + image.PhysicalDimension.Height + ".');";
                ScriptManager.RegisterStartupScript(this, GetType(), "JScript", alert, true);
            }
            else
            {
                // Creating a complete relative path for storing the image. And also attaching the datetime stamp with the image name.
                string path = "~/waterMarkedImages/" + myguid + System.DateTime.Now.ToString("yyyy-MM-dd HHmmtt") + FileExtension;
                // Saving the Image.
                FileUpload1.SaveAs(Server.MapPath(path));
                // Saving the Watermarked Image in the specified folder
                newImage.Save(Server.MapPath(path));
                // Assigning the uploaded image url to the Image control.
                Image1.ImageUrl = path;
                graphics1.Dispose();
                if (!string.IsNullOrEmpty(Image1.ImageUrl))
                {
                    // Showing a notification of success after uploading.
                    string alert = "alert('Image Uploaded Successfully');";
                    ScriptManager.RegisterStartupScript(this, GetType(), "JScript", alert, true);

                }
            }
        }

    }

No comments:

Post a Comment