I'd like to add a copyright message on images shown on my website.
I'd like to do this on the fly when they are shown on the webpage (so the files on the server are not changed), and this in a way that when people download the image, the watermark remains in the image.
Here is what I have been using for a while now. It works fine for me (I'm using the .NET Framework 1.0, however):
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Image;
public void AddWatermark(string filename, string watermarkText, Stream outputStream) {
Bitmap bitmap = Bitmap.FromFile(filename);
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.FromArgb(10, 0, 0, 0); //Adds a black watermark with a low alpha value (almost transparent).
Point atPoint = new Point(100, 100); //The pixel point to draw the watermark at (this example puts it at 100, 100 (x, y)).
SolidBrush brush = new SolidBrush(color);
Hope that works for you. I haven't actually tested this out as it's adapted from my code, but it looks okay!
[UPDATE]
Fixed some typos in the code above ... I noticed that I left some of my old variable names in there (_bitmap versus bitmap).
Also, the code contained in the catch block takes care of an exception that can be raised which states "A Graphics object cannot be created from an image that has an indexed pixel format." In that case, you need to create a Graphics instance from a blank bitmap
of the same dimensions, and then draw the old bitmap onto the canvas. I ran into this problem when opening a GIF image and attempting to add the watermark. I found the catch code on CodeProject somewhere, although I don't have the URL handy ...
[/UPDATE]
This may be a little old but i need code like this
tried to convert it to vb and i get some problems
In the graphics.drawstring and bitmap.save on the last few lines
heres what i came up with
Sub AddWatermark(ByVal filename
As String,
ByVal watermarkText
As String,
ByVal outputStream
As Stream) '
Dim myBitmap
As Bitmap = New Bitmap(filename)
Dim font As
New Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel)
Dim color As Color = Color.FromArgb(10, 0, 0, 0)
'Adds a black watermark with a low alpha value (almost transparent).
Dim atPoint
As New Point(100, 100)
'The pixel point to draw the watermark at (this example puts it at 100, 100 (x, y)).
Dim brush As
New SolidBrush(color)
Dim graphics
As Graphics = Nothing
Try
I think you figured it ou by now, but just in case, instead of the System.Drawing.Text object, you should place a string value. The same happens with the Save() function so casting the outputStream as string by using the .ToString() function should solve
your problems...
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y:
hidden;" id="_mcePaste">Hey,</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">I am using the following code, its working fine.</div> <div></div>
Hey,
I am using the following code, its working fine.
<div><div>
Public Sub AddWatermark(ByVal inStream As FileStream, ByVal watermarkText As String, ByVal outStream As Stream)
Dim newImg As Drawing.Image = Drawing.Image.FromStream(inStream)
Dim font As New Font("Tahoma", 12, FontStyle.Bold, GraphicsUnit.Pixel)
'Adds a transparent watermark with an 100 alpha value.
Dim color As Color = Drawing.Color.FromArgb(50, 0, 0, 0)
'The position where to draw the watermark on the image
Dim point As New Point(10, 10)
Dim myBrush As New SolidBrush(color)
Dim gr As Graphics = Nothing
Try
gr = Graphics.FromImage(newImg)
Catch
Dim img1 As Drawing.Image = newImg
newImg = New Bitmap(newImg.Width, newImg.Height)
gr = Graphics.FromImage(newImg)
gr.DrawImage(img1, New Rectangle(0, 0, newImg.Width, newImg.Height), 0, 0,newImg.Width, newImg.Height, GraphicsUnit.Pixel)
img1.Dispose()
End Try
gr.DrawString(watermarkText, font, myBrush, point)
gr.Dispose()
img.Save(outStream, ImageFormat.Jpeg)
End Sub
</div><div>I think it may be useful for you, my friends!</div><div>
</div><div>Ramesh Bandi</div><div>
</div></div>
Speerman
Contributor
2145 Points
455 Posts
Adding on the fly watermark to images
Jul 14, 2005 08:11 AM|LINK
I'd like to add a copyright message on images shown on my website.
I'd like to do this on the fly when they are shown on the webpage (so the files on the server are not changed), and this in a way that when people download the image, the watermark remains in the image.
I tried the following code: http://www.dnzone.com/ShowDetail.asp?NewsId=1397
but I keep getting a red cross in stead of my watermarked picture.
Does anyone know a good, freeware, solution to accomplish this?
Thanks in advance!
jethompson
Member
100 Points
18 Posts
Re: Adding on the fly watermark to images
Jul 14, 2005 09:34 PM|LINK
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Image;
public void AddWatermark(string filename, string watermarkText, Stream outputStream) {
Bitmap bitmap = Bitmap.FromFile(filename);
Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);
Color color = Color.FromArgb(10, 0, 0, 0); //Adds a black watermark with a low alpha value (almost transparent).
Point atPoint = new Point(100, 100); //The pixel point to draw the watermark at (this example puts it at 100, 100 (x, y)).
SolidBrush brush = new SolidBrush(color);
Graphics graphics = null;
try {
graphics = Graphics.FromImage(bitmap);
} catch {
Bitmap temp = bitmap;
bitmap = new Bitmap(bitmap.Width, bitmap.Height);
graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(temp, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
temp.Dispose();
}
graphics.DrawString(text, font, brush, atPoint);
graphics.Dispose();
bitmap.Save(outputStream);
}
To call it, just add something like this to your aspx file (assume the class is called MyImage):
string filename = Request.MapPath(Request.QueryString["filename"]);
MyImage image = new MyImage();
image.AddWatermark(filename, "Watermark Test", HttpResponse.OutputStream);
Hope that works for you. I haven't actually tested this out as it's adapted from my code, but it looks okay!
[UPDATE]
Fixed some typos in the code above ... I noticed that I left some of my old variable names in there (_bitmap versus bitmap).
Also, the code contained in the catch block takes care of an exception that can be raised which states "A Graphics object cannot be created from an image that has an indexed pixel format." In that case, you need to create a Graphics instance from a blank bitmap of the same dimensions, and then draw the old bitmap onto the canvas. I ran into this problem when opening a GIF image and attempting to add the watermark. I found the catch code on CodeProject somewhere, although I don't have the URL handy ...
[/UPDATE]
Speerman
Contributor
2145 Points
455 Posts
Re: Adding on the fly watermark to images
Jul 15, 2005 06:48 AM|LINK
this works perfect, thanks!
codegalaxy
Contributor
6910 Points
1475 Posts
Re: Adding on the fly watermark to images
Sep 28, 2005 02:40 PM|LINK
tried to convert it to vb and i get some problems
In the graphics.drawstring and bitmap.save on the last few lines
heres what i came up with
Sub AddWatermark(ByVal filename As String, ByVal watermarkText As String, ByVal outputStream As Stream) ' Dim myBitmap As Bitmap = New Bitmap(filename) Dim font As New Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel) Dim color As Color = Color.FromArgb(10, 0, 0, 0) 'Adds a black watermark with a low alpha value (almost transparent). Dim atPoint As New Point(100, 100) 'The pixel point to draw the watermark at (this example puts it at 100, 100 (x, y)). Dim brush As New SolidBrush(color) Dim graphics As Graphics = Nothing Try
graphics = graphics.FromImage(myBitmap)
Catch End Trygraphics.DrawString(System.Drawing.Text, font, brush, atPoint) <--problem
graphics.Dispose()
myBitmap.Save(outputStream) <--problem
End Sub 'AddWatermarkAnyone help me figure that one out?
TIA
read my stupid blog http://codemypantsoff.com
georgef
Member
2 Points
1 Post
Re: Adding on the fly watermark to images
Feb 19, 2008 10:57 AM|LINK
I think you figured it ou by now, but just in case, instead of the System.Drawing.Text object, you should place a string value. The same happens with the Save() function so casting the outputStream as string by using the .ToString() function should solve your problems...
Sanjeev.Kejr...
Member
73 Points
347 Posts
Re: Adding on the fly watermark to images
Feb 06, 2009 01:21 PM|LINK
this code is not working . please let me know how to use this. thanks
ramadurai ja...
Member
17 Points
113 Posts
Re: Adding on the fly watermark to images
Apr 13, 2009 09:17 AM|LINK
HI
Thank U for Your replay. But I want to add water mark to any file type such as .doc,.txt,.pdf....
dhol.gaurav
Contributor
3998 Points
725 Posts
Re: Adding on the fly watermark to images
Aug 19, 2009 08:57 AM|LINK
Thanks
It's Working
Nice One
Gaurav Dhol
Skype ID : dhol.gaurav
If My Post contains helped you, Please Mark as Answer
Ramesh Bandi
Member
20 Points
10 Posts
Re: Adding on the fly watermark to images
Oct 28, 2009 02:17 PM|LINK
Hey,
I am using the following code, its working fine.
<div><div>Public Sub AddWatermark(ByVal inStream As FileStream, ByVal watermarkText As String, ByVal outStream As Stream) Dim newImg As Drawing.Image = Drawing.Image.FromStream(inStream) Dim font As New Font("Tahoma", 12, FontStyle.Bold, GraphicsUnit.Pixel) 'Adds a transparent watermark with an 100 alpha value. Dim color As Color = Drawing.Color.FromArgb(50, 0, 0, 0) 'The position where to draw the watermark on the image Dim point As New Point(10, 10) Dim myBrush As New SolidBrush(color) Dim gr As Graphics = Nothing Try gr = Graphics.FromImage(newImg) Catch Dim img1 As Drawing.Image = newImg newImg = New Bitmap(newImg.Width, newImg.Height) gr = Graphics.FromImage(newImg) gr.DrawImage(img1, New Rectangle(0, 0, newImg.Width, newImg.Height), 0, 0,newImg.Width, newImg.Height, GraphicsUnit.Pixel) img1.Dispose() End Try gr.DrawString(watermarkText, font, myBrush, point) gr.Dispose() img.Save(outStream, ImageFormat.Jpeg) End Sub</div><div>I think it may be useful for you, my friends!</div><div>
</div><div>Ramesh Bandi</div><div>
</div></div>
MeDammit
Member
28 Points
4 Posts
Re: Adding on the fly watermark to images
Nov 22, 2009 04:49 PM|LINK
Changed
img.Save(outStream, ImageFormat.Jpeg)
to
newImg.Save(outStream, ImageFormat.Jpeg)
Above bold changed... Just in case nobody caught it and to pre-emptively avoid comments saying it doesn't work.
BTW, thank you!