Last post Aug 29, 2013 05:52 AM by Alaaeddin
Sep 11, 2011 03:41 PM|Shivanand Arur|LINK
Hello, i am developing a CMS based website where i am allowing the admin to add images that he likes, but i also want to provide the admin a facility to add watermark on his image dynamically while uploading the image!!! Please Help me with some examples...
Thanks in advance!!!
Sep 11, 2011 05:09 PM|smirnov|LINK
Sep 12, 2011 03:42 AM|Shivanand Arur|LINK
Hello smirnov, thanks for posting... but i have found the solution and it is working well for me!!! I am posting the code that i have written which is as follows:-
protected void AddImg_btn_Click(object sender, EventArgs e)
String csName = "ButtonClickScript";
Type csType = this.GetType();
// Adding watermark to the image and saving it into the specified folder!!!!
string path = "~/AbtusImg/" + FileUpload1.PostedFile.FileName;
Image image = Image.FromStream(FileUpload1.PostedFile.InputStream);
int width = image.Width;
int height = image.Height;
Bitmap bmp = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage((Image)bmp);
graphics.InterpolationMode = InterpolationMode.High;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, width, height);
Font font = new Font("Arial", 8, FontStyle.Bold);
SolidBrush brush = new SolidBrush(Color.FromArgb(100, 255, 222, 173));
graphics.DrawString("CCS Systems", font, brush, 0.0F, 75.0F); // string, font style, brush used, x and y position for the string to be written
Image newImage = (Image)bmp;
newImage.Save(Server.MapPath("~/AbtusImg/" + FileUpload1.PostedFile.FileName));
// Inserting the image and its data in the Database Table AboutUsImages...
cmd = new SqlCommand("Insert into AboutUsImages(Img_Address, Img_Name, Img_Data)" + "values(@img_address, @img_name, @img_data)", con);
cmd.Parameters.Add("@img_address", SqlDbType.VarChar).Value = path;
cmd.Parameters.Add("@img_name", SqlDbType.VarChar).Value = FileUpload1.PostedFile.FileName;
cmd.Parameters.Add("@img_data", SqlDbType.VarChar).Value = TextBox2.Text;
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>");
This is the code, i have written in the add image button event!!! It is working for me and i hope it will help others as well!!! Thanks
Aug 29, 2012 08:38 AM|sathya subramanian|LINK
Thanks for your code,
By using your code i can generate the watermark only in horizontal position. how can i get a vertical watermark.
Aug 29, 2012 08:45 AM|Shivanand Arur|LINK
Hey Sathya, I haven't tried it, since it was not my requirement. But you can play around with the the X and Y co-rodinates in the code...
graphics.DrawString("CCS Systems", font, brush, 0.0F, 75.0F);
You can change its value and check it. 0.0F is for the X Co-ordinate and 75.0F is for the Y-Co-ordinate.... If you are done with it, let me also know about....
Sep 28, 2012 01:08 AM|sathya subramanian|LINK
Your runs perfectly in localhost when i moved it to online server i m recieving an error "A generic error occurred in gdi+". How can i recover from this?
Sep 28, 2012 01:23 AM|Shivanand Arur|LINK
Hey Sathya, The folder in which you are saving your images should have "All Access" or "Read/Write" permissions.... Please check the permissions of the folder stored on the server...
This error generally occurs when you are trying to save a file at a place where you do not have proper permissions
Aug 29, 2013 05:52 AM|Alaaeddin|LINK
Dispose before saving the watermarked image will solve error occured in GDI+ (If you're using the same file name for both images)