View Full Version : Image stamps
CurtWRC
06-26-2006, 02:20 AM
When uploading an image with the <asp:FileUpload> control (ASP.NET 2.0), is it possible to add a watermark image (or text) to the image itself? For example, here is one I did manually on Photoshop:
http://rallystuff.net/uploads/2006MexicoKronosLoeb001.jpg
Is there any way of doing this?
Thanks,
Curt.
otaku149
06-26-2006, 02:48 PM
Hi Curt,
First you need to upload the image and than after create another image and add the watermark and save it. Finally delete the first image.
In the previous code I gave you, change that line:
FileUpload1.SaveAs(serverPath & fileNameOnServer)
By this one:
FileUpload1.SaveAs(serverPath & "temp-" & fileNameOnServer)
Also add the namespace System.Drawing in your web.config
Here is the complete code with the watermark modifications:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If FileUpload1.HasFile Then
Dim serverPath As String = Server.MapPath("avatars/")
Dim fileToUpload As String = FileUpload1.PostedFile.FileName
Dim fileExtension As String = Path.GetExtension(fileToUpload)
Dim fileNameOnServer As String = "avatar-" & User.Identity.Name & fileExtension
Dim fileContentType As String = FileUpload1.PostedFile.ContentType
Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
If Not InStr(fileContentType.ToLower(), "image") = 1 Then
Dim errMsg As String = "Error, only .gif .jpg or .png are allowed."
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = errMsg
AlertMsg.Display(Me.Page, "ErrorContentType", errMsg)
Return
End If
If fileSize > 20000 Then
Dim errMsg As String = "Error, 20 kb maximum."
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = errMsg
AlertMsg.Display(Me.Page, "ErrorContentLength", errMsg)
Return
End If
Try
FileUpload1.SaveAs(serverPath & "temp-" & fileNameOnServer)
Catch ex As Exception
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = ex.Message.ToString()
AlertMsg.Display(Me.Page, "ErrorException", ex.Message.ToString())
End Try
Dim waterMark As String = "© YourDomain.com"
Dim bmp As Bitmap
Dim fs As New System.IO.FileStream(serverPath & "temp-" & fileNameOnServer, System.IO.FileMode.Open)
bmp = System.Drawing.Image.FromStream(fs)
Dim cwm As Graphics = Graphics.FromImage(bmp)
cwm.DrawString(waterMark, New Font("Arial", 12, FontStyle.Bold), New SolidBrush(Color.Black), 0, 0)
Try
bmp.Save(serverPath & fileNameOnServer)
Catch ex As Exception
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = ex.Message.ToString()
AlertMsg.Display(Me.Page, "ErrorException", ex.Message.ToString())
Finally
fs.Close()
End Try
Dim tempFileToDelte As FileInfo
tempFileToDelte = New FileInfo(serverPath & "temp-" & fileNameOnServer)
tempFileToDelte.Delete()
lblMsg.Attributes.Add("style", "color:Black;")
lblMsg.Text = "We received your image, thank you!"
Else
Dim errMsg As String = "Error, Please browse and select a file to upload."
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = errMsg
AlertMsg.Display(Me.Page, "ErrorEmpty", errMsg)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Upload Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" AlternateText="You cannot upload files" />
<asp:Button ID="btUpload" runat="server" OnClick="btUpload_Click" Text="Upload" />
<br />
<asp:Label ID="lblMsg" runat="server" Font-Bold="True"></asp:Label>
</div>
</form>
</body>
</html>
To set the watermark text:
Dim waterMark As String = "© YourDomain.com"
To change the color, font-size, font-family and position (the first 0 is the X position and the second 0 is the Y position of the text over the image):
cwm.DrawString(waterMark, New Font("Arial", 12, FontStyle.Bold), New SolidBrush(Color.Black), 0, 0)
CurtWRC
06-26-2006, 02:56 PM
Thanks Martial, thats great :D
I didn't even think it would be possible. Is there anyway of using an image instead (a .gif with a transparent background) so that my websites logo can be used as a watermark? This one is most probably impossible I'm guessing :).
otaku149
06-26-2006, 04:01 PM
I think it's possible.
I have to make few test before, is it possible for you to give me 2 images:
your logo (to prevent white stroke, use transparent .png instead of transparent .gif)
a sample image (without the watermark)
Just give the 2 urls something like below:
http://www.rallystuff.net/images/logo.png
http://www.rallystuff.net/images/sampleimage.jpg
CurtWRC
06-26-2006, 05:30 PM
Image: http://rallystuff.net/uploads/2006italyKronosLoeb002.jpg
Logo: http://rallystuff.net/curt/images/logo.png
If it isn't possible or is too much hard work to figure out then just leave it as I quite like the text version as well as I can include content such as the title of the image, date etc.
Curt.
otaku149
06-26-2006, 06:09 PM
It work perfectly but your logo you gave me has a resolution of 300 pixel/inch so the logo appear very very very small and the jpg you gave me has a resolution of 96 pixel/inch.
To get the resolution of an image, open photoshop, open the image and go to the top menu "Image" + "Image size..", you will see the resolution.
Please give me both images with a resolution of 72 pixel/inch or at least with the same resolution. 72 pixel/inch is the web standard. Also make sure that both images has the correct size too. Let's suppose your sample jpg image has a size of 300px x 200px, your logo should have a size around 220px x 40px or something.
CurtWRC
06-26-2006, 06:38 PM
Would this be why some of the images Ive tried uploading recently have different sized fonts:
Huge: http://rallystuff.net/curt/uploads/gallery-5.gif
Small: http://rallystuff.net/curt/uploads/gallery-3.gif
otaku149
06-26-2006, 06:50 PM
Would this be why some of the images Ive tried uploading recently have different sized fonts:
Yes, that's probably why
otaku149
06-26-2006, 06:52 PM
If you wish to add the date, uploaded by..., the first solution with the text will be better.
CurtWRC
06-26-2006, 07:06 PM
If you wish to add the date, uploaded by..., the first solution with the text will be better.
Yeah, i think I will stick with the text version for at least a while. My only problem with it is if an image with a mainly white background is uploaded, and the watermark is white, then the watermark won't be seen. Is there anyway of created a back 'stroke' like on photoshop?
otaku149
06-26-2006, 09:06 PM
You can add background color something like a blue background color and text in white.
otaku149
06-26-2006, 09:29 PM
Here is the code for you to test. I added a MultiLine textbox txbMessage to display the message. To have line break, simple press enter or put text in the textbox like below:
© Curt.Rallystuff.net
Uploaded by: Curt
Date: 6/26/2006
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If FileUpload1.HasFile Then
Dim serverPath As String = Server.MapPath("photos/")
Dim fileToUpload As String = FileUpload1.PostedFile.FileName
Dim fileExtension As String = Path.GetExtension(fileToUpload)
Dim fileNameOnServer As String = Guid.NewGuid().ToString().Substring(0, 6) & fileExtension
Dim fileContentType As String = FileUpload1.PostedFile.ContentType
Dim fileSize As Integer = FileUpload1.PostedFile.ContentLength
If Not InStr(fileContentType.ToLower(), "image") = 1 Then
Dim errMsg As String = "Error, only .gif .jpg or .png are allowed."
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = errMsg
AlertMsg.Display(Me.Page, "ErrorContentType", errMsg)
Return
End If
If fileSize > 500000 Then
Dim errMsg As String = "Error, 500 kb maximum."
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = errMsg
AlertMsg.Display(Me.Page, "ErrorContentLength", errMsg)
Return
End If
Try
FileUpload1.SaveAs(serverPath & "temp-" & fileNameOnServer)
Catch ex As Exception
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = ex.Message.ToString()
AlertMsg.Display(Me.Page, "ErrorException", ex.Message.ToString())
End Try
Dim waterMark As String = txbMessage.Text
Dim fontCounter As Font = New Font("Verdana", 8, FontStyle.Bold)
Dim bmp As Bitmap
Dim fs As New System.IO.FileStream(serverPath & "temp-" & fileNameOnServer, System.IO.FileMode.Open)
bmp = System.Drawing.Image.FromStream(fs)
Dim cwm As Graphics = Graphics.FromImage(bmp)
Dim stringSize As SizeF = cwm.MeasureString(waterMark, fontCounter)
Dim nWidth As Integer = CType(stringSize.Width, Integer)
Dim nHeight As Integer = CType(stringSize.Height, Integer)
cwm.FillRectangle(New SolidBrush(Color.Blue), New Rectangle(0, 0, nWidth, nHeight))
cwm.DrawString(waterMark, fontCounter, New SolidBrush(Color.White), 0, 0)
Try
bmp.Save(serverPath & fileNameOnServer)
Catch ex As Exception
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = ex.Message.ToString()
AlertMsg.Display(Me.Page, "ErrorException", ex.Message.ToString())
Finally
fs.Close()
End Try
Dim tempFileToDelte As FileInfo
tempFileToDelte = New FileInfo(serverPath & "temp-" & fileNameOnServer)
tempFileToDelte.Delete()
lblMsg.Attributes.Add("style", "color:Black;")
lblMsg.Text = "We received your image, thank you!"
Else
Dim errMsg As String = "Error, Please browse and select a file to upload."
lblMsg.Attributes.Add("style", "color:Red;")
lblMsg.Text = errMsg
AlertMsg.Display(Me.Page, "ErrorEmpty", errMsg)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Upload Photos with Watermark</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txbMessage" runat="server" Height="128px" TextMode="MultiLine" Width="389px"></asp:TextBox>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" AlternateText="You cannot upload files" />
<asp:Button ID="btUpload" runat="server" OnClick="btUpload_Click" Text="Upload" />
<br />
<asp:Label ID="lblMsg" runat="server" Font-Bold="True"></asp:Label></div>
</form>
</body>
</html>
CurtWRC
06-27-2006, 04:39 PM
Thanks Martial.
Is there also a way of resizing images when uploading so that thumbnails can be created? I would just resize the properties in the <img> tag but it distorts the image.
otaku149
06-27-2006, 11:44 PM
My suggestion is to create 2 images because most users will upload images with big size such as 1600px X 1200px (200kb and more) with bad resolution and that kind of image is too big and need to be compressed and optimized for Web use. The resolution must be changed to 72 pixel/inch.
The program can create 2 images compressed and optimized with 72 pixel/inch resolution, the first may have a width of 600px (the big one) and the second one may have a width of 300px (the small one). The height is not important because the program will calculate the appropriate height depending of the width in order to not distort the image. Both images will have a stamps like "© Curt.Rallystuff.net" with a black background color and white text color.
Created dynamically, images names will look something like below:
small-68692F.jpg
big-68692F.jpg
small-ABG4K8.jpg
big-ABG4K8.jpg
Now to display those images, you don't need any database if you wish because all images will be located in the same directory, you can display all the small images into a datalist and if the visitor click on the small image this will open a popup with the big image. When the visitor will mouseover the image, we can display the uploaded date using the HTML alt attribute. Also we can add a custom pagination in that datalist.
Let me know what you want to do exactly...
CurtWRC
06-28-2006, 08:17 PM
I was just wondering whether this would be possible to do as Im going to need to do this on my new site. The ability to change the resolution is quite useful as well.
otaku149
06-29-2006, 02:05 AM
Are you going to be the only person who will upload photos or anybody will be able to upload photos ?
CurtWRC
06-29-2006, 03:23 PM
Are you going to be the only person who will upload photos or anybody will be able to upload photos ?
On the new site there will be a membership system so that members can upload photos taken at rallies (This site will be different to the blog site).
otaku149
06-29-2006, 09:33 PM
Hello Curt,
Here is the project for you to download:
http://www.getelementbyid.com/demo/CurtWRC.zip
PhotoDataSource Class
Find this below line in display.aspx, this is the path where your photos are located:
PhotoData.DirPath = Server.MapPath("../photos/")
When visitors will click on the small image, the big image will appear in the popup. The popup will be automatically centered on the screen whatever the width and the height of the image. The visitors may click anywhere on that big image to close the popup.
ImageCreator Class
Find these below lines in upload.aspx
Dim smallImgName As String = serverPath & "small-" & fileName
Dim bigImgName As String = serverPath & "big-" & fileName
Dim SmallImage As New ImageCreator(filePath, 250, "© Curt.Rallystuff.net", 10, smallImgName)
SmallImage.Create()
Dim BigImage As New ImageCreator(filePath, 600, "© Curt.Rallystuff.net", 14, bigImgName)
BigImage.Create()
When you create an image (JPG only), you first need to create an instance of the ImageCreator class with some constructor parameters.
Example:
Dim SmallImage As New ImageCreator(filePath, 250, "© Curt.Rallystuff.net", 10, smallImgName)
filePath => the file path of the image that we use to create the new image
250 => the width of the new image
© Curt.Rallystuff.net => the watermark text
10 => the font-size of the watermark text
smallImgName => the new image path and name
After you need to use the Create method to create the new image:
SmallImage.Create()
If you choose to create and image with a width of 600px, you need to upload an image with a width of 600px minimum. It's like photoshop, you won't get good result if you try to maximize an image.
Run the project and let me know if everything is fine. My summer holiday start soon.
otaku149
06-29-2006, 10:37 PM
I forgot to mention that the newest photo uploaded will be displayed first in display.aspx
If you would like to display the newest photo uploaded at the end, find the following line in the PhotoDataSource Class and change "CreationTime desc" by "CreationTime asc"
View.Sort = "CreationTime desc"
Also the datalist pagination actually display 4 photos per page, to change that value find the following line in display.aspx
PagedData.PageSize = 4
CurtWRC
07-02-2006, 12:39 PM
Thanks Martial. I didn't think you were going to create a whole project! :eek:
otaku149
07-02-2006, 11:24 PM
Thanks Martial.
You're welcome Curt :)
I didn't think you were going to create a whole project!
I just wanted to give you a good start, you still have a lot of job to do like the membership feature, design etc...
Best Regards,
CurtWRC
07-18-2006, 05:54 PM
Today is the first time I have used the resizing project, and it has made the quality look really bad on the images, unlike the ones in the photos folder within the project itself. I changed the widths to resize to to 150px and 70px. Here is what the 150px looks like:
http://img194.imageshack.us/img194/4076/news1hq3.jpg
As you can see the quality isnt great. Why is mine doing this?
Thanks.
otaku149
08-30-2006, 01:53 AM
It's hard to say, maybe poor quality pics or camera, also maybe you try to resize too small for that pic. If you resize the original image using photoshop you will probably get the same result for that pic.
You can try to change the resolution in the ImageCreator Class:
b.SetResolution(72, 72)
For the copyright text, try to change the font size:
Dim SmallImage As New ImageCreator(filePath, 150, "© Curt.Rallystuff.net", 10, smallImgName)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.