add watermark in image

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

add watermark in image

 
0
  #1
Jan 18th, 2008
i want to add watermark in image.i am using image handler. its not properly working. i want to add watermark at runtime only no need for original image.in my home page i have 25 thumbnail images. when i click any one of the image it goes next page and display large image at that time only i need watermark in image.please reply as early as possible.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

 
1
  #2
Jan 18th, 2008
two effective ways to do this.. You can create a gif file that is transparent except for your watermark and create it the same size, or close to, of the default image. Then on your page, set the background of a div to the default image, and the size of the div to the size of the default image. Then inside the div, place the <img ...> of the watermark. This stops most people from copying and saving your pictures.

Anther way is to have it done dynamically at runtime. This creates a krap load of resources on your server and will always as long as any image is called. I would recommend staying away from it.

The way that I do it is that I upload the picture to my server through a website, save a temp of it, open the temp, resize and then add a watermark to it (just text at the moment).. then save it to the name and directory wanted. I do this both for thumbnails and main images. I'll give you a resize function and a watermark function that you might want to use.

You need these namespaces:
  1. <%@ import Namespace="System.IO" %>
  2. <%@ import Namespace="System.Drawing.Imaging" %>
  3. <%@ import Namespace="System.Drawing" %>
  4. <%@ import Namespace="System.Drawing.Drawing2D" %>
resize function:
  1. Function generateThumbnail(ByVal newWidth As Integer, ByVal strFileName As String, ByVal newStrFileName As String)
  2. Dim bmp As Bitmap
  3. Dim newHeight As Integer
  4. Dim resized As Bitmap
  5. Dim FilePath As String = Server.MapPath("/images/")
  6.  
  7. bmp = System.Drawing.Image.FromFile(FilePath & strFileName & ".jpg")
  8. newHeight = (newWidth/bmp.width)*bmp.Height
  9. resized = new Bitmap(newWidth,newHeight)
  10. newStrFileName = FilePath & newStrFileName & ".jpg"
  11.  
  12. Dim g as Graphics = Graphics.FromImage(resized)
  13. g.SmoothingMode = SmoothingMode.HighQuality
  14. g.CompositingQuality = CompositingQuality.HighQuality
  15. g.InterpolationMode = InterpolationMode.High
  16. g.DrawImage(bmp, new Rectangle(0,0,resized.Width,resized.Height),0,0,bmp.Width,bmp.Height,GraphicsUnit.Pixel)
  17. g.Dispose()
  18. resized.Save(newStrFileName,ImageFormat.Jpeg)
  19. bmp.dispose()
  20. End Function
watermark function:
  1. Function watermark(ByVal strFileName As String, ByVal newStrFileName As String)
  2. Dim FilePath As String = Server.MapPath("/images/")
  3. Dim bmp As Bitmap = System.Drawing.Image.FromFile(FilePath & strFileName & ".jpg")
  4. Dim strWatermark As String = "WATERMARK TEXT"
  5. Dim canvas As Graphics = Graphics.FromImage(resized)
  6. Dim StringSizeF As SizeF, DesiredWidth As Single, wmFont As Font, RequiredFontSize As Single, Ratio As Single
  7. newStrFileName = FilePath & newStrFileName & ".jpg"
  8. wmFont = New Font("Verdana", 6, FontStyle.Bold)
  9. DesiredWidth = bmp.Width * .75
  10. StringSizeF = canvas.MeasureString(strWatermark, wmFont)
  11. Ratio = StringSizeF.Width / wmFont.SizeInPoints
  12. RequiredFontSize = DesiredWidth / Ratio
  13. wmFont = New Font("Verdana", RequiredFontSize, FontStyle.Bold)
  14. canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.Beige), 0, 0)
  15. canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 0, 0, 0)), 2, 2)
  16. canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 255, 255, 255)), 0, 0)
  17. canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 0, 0, 0)), 2, 2)
  18. canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 255, 255, 255)), 0, 0)
  19. bmp.SetResolution(96, 96)
  20. bmp.Save(newStrFileName,ImageFormat.Jpeg)
  21. canvas.dispose()
  22. bmp.dispose()
  23. End Function
Now these functions I have edited before I put on here, so if there's a flaw let me know as I haven't had time to test them. Anyway, you need to save your uploaded image first, or at least know the location of the ones you want to watermark or resize. An example on how to use these are:
  1. ''resize
  2. ''if you wish to have the ".jpg" within the function command, remove it from
  3. ''within the function and you can have "resizeme.jpg" and "resized.jpg"
  4. generateThumbnail(100,"resizeme","resized")
  5.  
  6. ''watermark
  7. ''if you wish to have the ".jpg" within the function command, remove it from
  8. ''within the function and you can have "watermarkme.jpg" and "watermarked.jpg"
  9. watermark("watermarkme","watermarked")
These functions only do JPEGS, so if you try another file format, it will fail on you.
Last edited by SheSaidImaPregy; Jan 18th, 2008 at 10:51 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

 
0
  #3
Jan 21st, 2008
thanks.
i want watermark only at runtime.no need to save with watermark and also i am using .jpg,.gif,.tif,.png image formats.During image upload,resize functions are over.when clicking thumb image in first page it displays big image in second page. these are done. i need only one thing add watermark when display image thats the problem
Last edited by nandhinijp; Jan 21st, 2008 at 5:36 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

 
0
  #4
Jan 21st, 2008
for images at runtime, follow this article. it works and is a good way to do things, however I do not believe in putting so much process on the server everytime an image is requested. But that's my take on it

http://www.codeproject.com/KB/aspnet...geHandler.aspx
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

 
0
  #5
Jan 22nd, 2008
hi,
I enclosed one word doc.there are one class file and webconfig details.In that class file image path spcified like
Image image = Image.FromFile(context.Request.PhysicalPath);.
that means watermark image applied for all displayed images. its working very good.but i dont want to show watermark for all images. if i integrate that class file with my project its totally struck.. because my site like photo upload.please verify that class file and give solution.
Attached Files
File Type: doc ImageHandler.doc (33.0 KB, 22 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

 
0
  #6
Jan 22nd, 2008
So put all the coding within a function. Then you can call that function on the certain photos you want watermarked.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

 
0
  #7
Jan 27th, 2008
i couldnt get result please explain
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

 
0
  #8
Jan 27th, 2008
explain which one, the one I gave you or the one on the tutorial?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

 
0
  #9
Jan 28th, 2008
U told to put all the coding within a function. that class contains 3 fun and implements IHttpHandler.how to use these in .aspx page include IHttpHandler.i want one and only thing thats at runtime just i click the thumb and corresponding large image will load with watermark.if any other methods to put just watermark in <img> tag.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

 
0
  #10
Jan 28th, 2008
well your web.config file is being called everytime your page is. Within your web.config file, you have the ImageHandler class being called for every .jpg, etc. If you want only the large image to watermark, then I would suggest creating a unique string within the images' name so when it is called, ONLY that image will be watermarked. An example of this:
  1. Large Image Names:
  2. terry-123WM123.jpg
  3. beachparty-123WM123.jpg
  4. howdy-123WM123.jpg
  5.  
  6. or
  7.  
  8. picture123WM123.jpg
  9. terry123WM123.jpg
  10.  
  11. etc.
  12.  
  13. Now in your web.config file, change your HTTP Handler that you put in there
  14. [
  15. <httpHandlers>
  16. <add verb="*" path="*.jpg,*.gif" type="ImageHandler,Home" validate="false"/>
  17. </httpHandlers>
  18. ]
  19. to
  20. [
  21. <httpHandlers>
  22. <add verb="*" path="*123WM123.jpg,*123WM123.gif" type="ImageHandler,Home" validate="false"/>
  23. </httpHandlers>
  24. ]
  25.  
  26. The star (*) symbolizes that anything with a jpg or gif end tag will be run with the watermark code. Therefore, if you only want specific images to contain the watermark, add a special unique string within the files being called, much like "123WM123". This will call anything that ends with "123WM123.jpg" and "123WM123.gif". Now I suggest doing something more practical like:
  27. *WML.jpg and *WML.gif. WML meaning watermarklarge (for me)
Do you understand?
Last edited by SheSaidImaPregy; Jan 28th, 2008 at 10:23 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC