Watermarking Code works, but not if used in codebehind file?

Last post 11-06-2009 11:13 PM by OneIdesigned. 4 replies.

Sort Posts:

  • Watermarking Code works, but not if used in codebehind file?

    11-01-2009, 5:51 PM

    Puzzled why the same Sub Page_Load code will not work if used on the codebehind file. Current code works fine if used directly on the aspx page,  but as soon as i try placing the same code in a codebehind file the images will not show at all. Will post code sample if requested..

    The way the code is setup thumbnail page opens a new page that displays larger image with watermark. the watermark page only contains code that creates the watermark for the larger image page.

     

    Thanks inadvance for any suggestions

    1idesigned

  • Re: Watermarking Code works, but not if used in codebehind file?

    11-01-2009, 6:01 PM
    • Member
      638 point Member
    • losssoc
    • Member since 04-24-2008, 9:53 AM
    • Posts 212

    Could you post your code sample?




  • Re: Watermarking Code works, but not if used in codebehind file?

    11-04-2009, 10:51 PM

    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

  • Re: Watermarking Code works, but not if used in codebehind file?

    11-05-2009, 4:23 PM
    Answer
    • Contributor
      6,114 point Contributor
    • atconway
    • Member since 09-24-2007, 9:20 PM
    • Florida U.S.A
    • Posts 1,259

    I think I have found the problem.  If you tried debugging the code you attempted to place in the page behind .vb file, I think you would find it does not even get ran.  The reason has to do with how you copied and pasted the signature of the 'Page_Load' Sub().  That signature works fine using script tags in the Page's source, but if you place it in the page behind code, you need to wire it up to an event unsing the VB.NET 'Handles' statement.

    So the following code in the page behind which currently does not work:

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

     

    ...needs to be changed to the following code which assigns a 'Handles' stament, telling .NET to run that code when the page is loaded:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    By the way, the easiest way to get the proper event handlers for page and control events is to use the (2) dropdowns at the top of the IDE showing the code in VS.NET.  So for Page_Load (which is typically auto generated and already exists) you could select '(Page Events)' from the dropdown on the left hand screen dropdown and 'Load' from the right-hand dropdown.  This would create the method signature if it did not exist, or jump to it, if it did not yet exist yet.

    Hope this helps! Smile
     

    Thank you,   >[Blog]<

    "The best thing about a boolean is even if you are wrong, you are only off by a bit." :D
    -anonymous

  • Re: Watermarking Code works, but not if used in codebehind file?

    11-06-2009, 11:13 PM
    Answer

    That was the answer. Thank you for your time. I really appreciate it. 

Page 1 of 1 (5 items)