User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 402,069 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,598 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 5497 | Replies: 47
Reply
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

add watermark in image

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

  #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:
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>
resize function:
Function generateThumbnail(ByVal newWidth As Integer, ByVal strFileName As String, ByVal newStrFileName As String)
  Dim bmp As Bitmap
  Dim newHeight As Integer
  Dim resized As Bitmap
  Dim FilePath As String = Server.MapPath("/images/")

  bmp = System.Drawing.Image.FromFile(FilePath & strFileName & ".jpg")
  newHeight = (newWidth/bmp.width)*bmp.Height
  resized = new Bitmap(newWidth,newHeight)
  newStrFileName = FilePath & newStrFileName & ".jpg"
  
  Dim g as Graphics = Graphics.FromImage(resized)
  g.SmoothingMode = SmoothingMode.HighQuality
  g.CompositingQuality = CompositingQuality.HighQuality
  g.InterpolationMode = InterpolationMode.High
  g.DrawImage(bmp, new Rectangle(0,0,resized.Width,resized.Height),0,0,bmp.Width,bmp.Height,GraphicsUnit.Pixel)
  g.Dispose()
  resized.Save(newStrFileName,ImageFormat.Jpeg)
  bmp.dispose()
End Function
watermark function:
Function watermark(ByVal strFileName As String, ByVal newStrFileName As String)
  Dim FilePath As String = Server.MapPath("/images/")
  Dim bmp As Bitmap = System.Drawing.Image.FromFile(FilePath & strFileName & ".jpg")
  Dim strWatermark As String = "WATERMARK TEXT"
  Dim canvas As Graphics = Graphics.FromImage(resized)
  Dim StringSizeF As SizeF, DesiredWidth As Single, wmFont As Font, RequiredFontSize As Single, Ratio As Single
  newStrFileName = FilePath & newStrFileName & ".jpg"
  wmFont = New Font("Verdana", 6, FontStyle.Bold)
  DesiredWidth = bmp.Width * .75
  StringSizeF = canvas.MeasureString(strWatermark, wmFont)
  Ratio = StringSizeF.Width / wmFont.SizeInPoints
  RequiredFontSize = DesiredWidth / Ratio
  wmFont = New Font("Verdana", RequiredFontSize, FontStyle.Bold)
  canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.Beige), 0, 0)
  canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 0, 0, 0)), 2, 2)
  canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 255, 255, 255)), 0, 0)
  canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 0, 0, 0)), 2, 2)
  canvas.DrawString(strWatermark, wmFont, New SolidBrush(Color.FromArgb(128, 255, 255, 255)), 0, 0)
  bmp.SetResolution(96, 96)
  bmp.Save(newStrFileName,ImageFormat.Jpeg)
  canvas.dispose()
  bmp.dispose()
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:
''resize
''if you wish to have the ".jpg" within the function command, remove it from
''within the function and you can have "resizeme.jpg" and "resized.jpg"
generateThumbnail(100,"resizeme","resized")

''watermark
''if you wish to have the ".jpg" within the function command, remove it from
''within the function and you can have "watermarkme.jpg" and "watermarked.jpg"
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 9:51 am.
Reply With Quote  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

  #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 4:36 am.
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

  #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  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

  #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, 8 views)
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

  #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  
Join Date: Dec 2007
Posts: 84
Reputation: nandhinijp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
nandhinijp nandhinijp is offline Offline
Junior Poster in Training

Re: add watermark in image

  #7  
Jan 27th, 2008
i couldnt get result please explain
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

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

Re: add watermark in image

  #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  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: add watermark in image

  #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:
Large Image Names:
terry-123WM123.jpg
beachparty-123WM123.jpg
howdy-123WM123.jpg

or

picture123WM123.jpg
terry123WM123.jpg

etc.

Now in your web.config file, change your HTTP Handler that you put in there
[
<httpHandlers> 
<add verb="*" path="*.jpg,*.gif" type="ImageHandler,Home" validate="false"/>
</httpHandlers>
]
to
[
<httpHandlers> 
<add verb="*" path="*123WM123.jpg,*123WM123.gif" type="ImageHandler,Home" validate="false"/>
</httpHandlers>
]

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:
*WML.jpg and *WML.gif. WML meaning watermarklarge (for me)
Do you understand?
Last edited by SheSaidImaPregy : Jan 28th, 2008 at 9:23 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 12:55 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC