Here is the code.
Code that works
<Code>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script language=vbscript runat=server >
Function ThumbnailCallback() As Boolean
Return False
End Function
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Get the image name – yourimage.jpg – from the query String
Dim imageURL As String = Request.QueryString("img")
Dim imageHeight As Integer
Dim Path As String = "Admin/IncomingPhotos/"
'Set the thumbnail width in px – the width will be calculated later to keep the original ratio.
Dim imageWidth As Integer = 800
Dim CurrentimgHeight As Integer
Dim CurrentimgWidth As Integer
imageURL = Server.MapPath(Path) & imageURL
Dim fullSizeImg As System.Drawing.Image
fullSizeImg = System.Drawing.Image.FromFile(imageURL)
CurrentimgHeight = fullSizeImg.Height
CurrentimgWidth = fullSizeImg.Width
imageHeight = CurrentimgHeight / CurrentimgWidth * imageWidth
'This will only work for jpeg images
Response.ContentType = "image/jpeg"
If imageHeight > 0 And imageWidth > 0 Then
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim thumbNailImg As System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(thumbNailImg)
Dim StringSizeF As SizeF, DesiredWidth As Single, wmFont As Font, RequiredFontSize As Single, Ratio As Single
Dim strWatermark As String = "WATERMARK"
'Set the watermark font
wmFont = New Font("Verdana", 16, FontStyle.Bold)
DesiredWidth = imageWidth * 0.8
'use the MeasureString method to position the watermark in the centre of the image
StringSizeF = g.MeasureString(strWatermark, wmFont)
Ratio = StringSizeF.Width / wmFont.SizeInPoints
RequiredFontSize = DesiredWidth / Ratio
wmFont = New Font("Verdana", RequiredFontSize, FontStyle.Bold)
'Sets the interpolation mode for a high quality image
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(fullSizeImg, 0, 0, imageWidth, imageHeight)
g.SmoothingMode = SmoothingMode.HighQuality
Dim letterBrush As SolidBrush = New SolidBrush(Color.FromArgb(50, 255, 255, 255))
Dim shadowBrush As SolidBrush = New SolidBrush(Color.FromArgb(50, 0, 0, 0))
'Enter the watermark text
g.DrawString("WATERMARK", wmFont, shadowBrush, 75, (imageHeight * 0.5) - 36)
g.DrawString("WATERMARK", wmFont, letterBrush, 77, (imageHeight * 0.5) - 38)
thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.Write(imageURL)
End If
'Important, dispose of the image – otherwise the image file will be locked by the server for several minutes
fullSizeImg.Dispose()
End Sub
</script>
Code Behind
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.IO
Public Class WaterMarkImage
Inherits System.Web.UI.Page
Function ThumbnailCallback() As Boolean
Return False
End Function
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Get the image name – yourimage.jpg – from the query String
Dim imageURL As String = Request.QueryString("img")
Dim imageHeight As Integer
Dim Path As String = "Admin/IncomingPhotos/"
'Set the thumbnail width in px – the width will be calculated later to keep the original ratio.
Dim imageWidth As Integer = 800
Dim CurrentimgHeight As Integer
Dim CurrentimgWidth As Integer
imageURL = Server.MapPath(Path) & imageURL
Dim fullSizeImg As System.Drawing.Image
fullSizeImg = System.Drawing.Image.FromFile(imageURL)
CurrentimgHeight = fullSizeImg.Height
CurrentimgWidth = fullSizeImg.Width
imageHeight = CurrentimgHeight / CurrentimgWidth * imageWidth
'This will only work for jpeg images
Response.ContentType = "image/jpeg"
If imageHeight > 0 And imageWidth > 0 Then
Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
Dim thumbNailImg As System.Drawing.Image
thumbNailImg = fullSizeImg.GetThumbnailImage(imageWidth, imageHeight, dummyCallBack, IntPtr.Zero)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(thumbNailImg)
Dim StringSizeF As SizeF, DesiredWidth As Single, wmFont As Font, RequiredFontSize As Single, Ratio As Single
Dim strWatermark As String = "WATERMARK"
'Set the watermark font
wmFont = New Font("Verdana", 16, FontStyle.Bold)
DesiredWidth = imageWidth * 0.8
'use the MeasureString method to position the watermark in the centre of the image
StringSizeF = g.MeasureString(strWatermark, wmFont)
Ratio = StringSizeF.Width / wmFont.SizeInPoints
RequiredFontSize = DesiredWidth / Ratio
wmFont = New Font("Verdana", RequiredFontSize, FontStyle.Bold)
'Sets the interpolation mode for a high quality image
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(fullSizeImg, 0, 0, imageWidth, imageHeight)
g.SmoothingMode = SmoothingMode.HighQuality
Dim letterBrush As SolidBrush = New SolidBrush(Color.FromArgb(50, 255, 255, 255))
Dim shadowBrush As SolidBrush = New SolidBrush(Color.FromArgb(50, 0, 0, 0))
'Enter the watermark text
g.DrawString("WATERMARK", wmFont, shadowBrush, 75, (imageHeight * 0.5) - 36)
g.DrawString("WATERMARK", wmFont, letterBrush, 77, (imageHeight * 0.5) - 38)
thumbNailImg.Save(Response.OutputStream, ImageFormat.Jpeg)
Else
fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.Write(imageURL)
End If
'Important, dispose of the image – otherwise the image file will be locked by the server for several minutes
fullSizeImg.Dispose()
End Sub
End Class
I call the watermark page using the following code, the watermark only works if i use the watermarked_thumbnail.aspx, same code placed in WaterMarkImage.aspx that is in the code behind will not work. It will not show any image.
<td rowspan="4" align="left" valign="middle" width="800px">
<asp:Image ID="fullSizeImg" runat="server" ImageUrl='<%# "watermarked_thumbnail.aspx?img=" & Eval("ImageFilePath") %>' Height="600px" Width="800px" ImageAlign="Middle" AlternateText='<%# "Title: " & Eval("Title") %>'
ToolTip='<%# "Title " & Eval("Title") %>' />
</td>
Hope this helps