Wednesday, 5 June 2013

Crop Image And Add water mark as a Image to image and Saved in Database and Filder also


Hello All,
I am mentioning here  the code to add watermark to the image Programmatically......You can use it.

  protected void btnupload_Click(object sender, EventArgs e)
    {
        String csName = "ButtonClickScript";
        Type csType = this.GetType();
        if (FileUpload1.HasFile)
        {
            try
            {
              
                string imageUrl = "~/UserImage/UserUploadImage/" + FileUpload1.PostedFile.FileName;
                //Fetching the image path of the Image that will be bind as Matermark
                string serverPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
                string imageIconUrl = serverPath + "\\img\\watermarkimage.png";
  
                    //Get the location of the image
                    Image imagePhoto = Image.FromStream(FileUpload1.PostedFile.InputStream);

                    // Get dimensions of image
                    int imageHeight = imagePhoto.Height;
                    int imageWidth = imagePhoto.Width;

                    //Create a new Bitmap
                    Bitmap oBitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format24bppRgb);

                    //Load Background Graphic from Image
                    Graphics oGraphics = Graphics.FromImage(oBitmap);
                    oGraphics.SmoothingMode = SmoothingMode.HighQuality;
                    oGraphics.DrawImage(imagePhoto, new Rectangle(0, 0, imageWidth, imageHeight), 0, 0, imageWidth, imageHeight, GraphicsUnit.Pixel);

                    //Layer 1: Add an Image Logo to the top left
                    if (!String.IsNullOrEmpty(imageIconUrl))
                    {
                        Image imageIcon = Image.FromFile(imageIconUrl);
                        oGraphics.DrawImage(imageIcon, new Rectangle(imageWidth-50, imageHeight-50, 35, 35), 0, 0, imageIcon.Width, imageIcon.Height, GraphicsUnit.Pixel);

                        imageIcon.Dispose();
                    }


   #region TO ADD THE TEXT AS WATER MARK
                    ////Layer 2: Add Comment
                    //if (!String.IsNullOrEmpty(imageComment))
                    //{
                    //    Font commentFont = new Font("Arial", 14, FontStyle.Regular); //Font Style
                    //    StringFormat commentFormat = new StringFormat();
                    //    commentFormat.Alignment = StringAlignment.Near; //Align text in left of layer

                    //    SolidBrush commentBrush = new SolidBrush(Color.Black); //Font Colour

                    //    oGraphics.FillRectangle(Brushes.Beige, 5, imageHeight - 55, imageWidth - 15, 50); //Create a rectangle with white background
                    //    oGraphics.DrawString(imageComment, commentFont, commentBrush, new Rectangle(5, imageHeight - 55, imageWidth - 15, 50), commentFormat); //Add comment text inside rectangle
                    //}

                    //Layer 3: Add Copyright watermark
                    //Font watermarkFont = new Font("Arial", 40, FontStyle.Bold); //Font Style
                    //SolidBrush semiTransBrush = new SolidBrush(Color.LightGray); //Font Colour
                    //StringFormat watermarkFormat = new StringFormat();
                    //watermarkFormat.Alignment = StringAlignment.Center; //Align text in center of image

                    //oGraphics.DrawString("Copyright",
                    //    watermarkFont,
                    //    semiTransBrush,
                    //    new PointF(imageWidth / 2, imageHeight / 2), watermarkFormat);
                    #endregion

                    //Dispose of graphic objects
                    imagePhoto.Dispose();
                    oGraphics.Dispose();

                    //Output image
                    string pageimageFolder = Server.MapPath(imageUrl);
                    if (File.Exists(pageimageFolder))
                    {
                        imageUrl = "";
                        imageUrl = "~/UserImage/" + DateTime.Now.Year + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + FileUpload1.FileName.ToString();
                        ////pageimageFolder = Server.MapPath(path);
                        oBitmap.Save(Server.MapPath(imageUrl));
                    }
                    oBitmap.Save(Server.MapPath(imageUrl));

                ClientScript.RegisterClientScriptBlock(csType, csName, "<script>alert('Image added successfully in the database and the folder!!!')</script>");
               
            }
            catch (Exception ex)
            {
                ClientScript.RegisterClientScriptBlock(csType, csName, "<script>alert('Some problem occured while adding the image!!! Please try again!!!')</script>");
                Response.Write(ex.Message);
            }
        }
    }

No comments:

Post a Comment