Important Notice from AspDotNetStorefront
It is with dismay that we report that we have been forced, through the action of hackers, to shut off write-access to this forum. We are keen to leave the wealth of material available to you for research. We have opened a new forum from which our community of users can seek help, support and advice from us and from each other. To post a new question to our community, please visit: http://forums.vortx.com
Results 1 to 8 of 8

Thread: Transparency + Watermark

  1. #1
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default Transparency + Watermark

    I'm having an issue where turning on the watermark feature in ML 8.0.1.2 will turn the background of my image black. This happens even if i don't specify any watermark image or watermark text.

    My image is a PNG that is specified using ImageFilenameOverride.
    Last edited by cjbarth; 06-25-2010 at 03:47 PM.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  2. #2
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    I found something odd when looking through the code in wolthuis.aspx.vb which is responsible for generating the watermark images. It seems that if the image type is PNG it is written to the OutputStream as a GIF.

    When I change that to PNG the image breaks. That is to say, in Chrome I get a 'broken picture link' and in Firefox I see nothing.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  3. #3
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    I looked up some other code that I've used to output images in .Net and have put that in wolthuis.aspx.vb and it seems to work.

    I removed the code that contains all the If ImgUrl.EndsWith statements and the imgPhoto.Dispose() and replaced it with the following block of code:

    Code:
    Response.ContentType = "image/png"
    
    Dim imgStream As New IO.MemoryStream
    imgPhoto.Save(imgStream, Drawing.Imaging.ImageFormat.Png)
    imgPhoto.Dispose()
    
    Dim imgBuffer As Byte() = imgStream.GetBuffer
    Response.OutputStream.Write(imgBuffer, 0, imgBuffer.Length)
    imgStream.Dispose()
    This code will always output a PNG image but it will completely read the image from memory into a byte array before flushing it to the OutputStream. This results in an image that outputs correctly as PNG. Though I'm still seeing a black image I see that the quality of the image is much better. The color depth is unmodified. This would make sense because a GIF image has much less color depth than a PNG, which is what my image originally was.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  4. #4
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    I was tracing the code through CommonLogic.vb and found another interesting thing. The variable bmPhoto was defined with a format that doesn't have support for an Alpha layer. Since bmPhoto is the Bitmap on which the image with its watermark is assembled this would prevent the outputted image from having a transparent background.

    I therefore changed the bmPhoto variable definition in AddWatermark from:

    Code:
    Dim bmPhoto As Bitmap = New Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb)
    to:

    Code:
    Dim bmPhoto As Bitmap = New Bitmap(phWidth, phHeight, PixelFormat.Format32bppArgb)
    Now my image is showing up with the proper transparency, but it seems to be showing two watermarks.
    Last edited by cjbarth; 06-25-2010 at 03:49 PM.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  5. #5
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    It seems that there are two parts to the AddWatermark routine. One adds text as a watermark and the other adds an image as a watermark.

    While the part to add an image as a watermark has an If...Then statement to prevent it from being run if there is no image, there is no such condition to prevent a text watermark from be applied if there is no text specified, but there is an image specified.

    The default behavior is that if there is no text specified then to apply a text watermark that is the name of the site. However, this is undesired for me because I've specified an image watermark and therefore don't want any text watermark.

    To solve this problem I've added the following line just before where the sizes variable is defined:

    Code:
    If imgWatermark Is Nothing Then
    I then ended this If block just before Step #2 starts.

    That prevents my image from having two watermarks on it.
    Last edited by cjbarth; 06-25-2010 at 03:54 PM.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  6. #6
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    I noticed something strange about some watermark images that I put up. Some weren't showing any green. After looking further at the code I figured out why.

    There is a block of code in AddWatermark in CommonLogic.vb that will convert all green the transparent. It seems the reason for this is to give images without any transparency some transparency. This would work like a green-screen. However, this isn't helpful to those of us that use watermarks that have transparency in them.

    The fix I devised is quite simple: if the watermark has transparency, don't try to convert the green to transparency. I therefore changed the following code:

    Code:
    Dim colorMap As ColorMap = New ColorMap()
    
    ...
    
    imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap)
    to be:

    Code:
    If Not Image.IsAlphaPixelFormat(bmWatermark.PixelFormat) Then
    	Dim colorMap As ColorMap = New ColorMap()
    
    ...
    
    	imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap)
    
    End If
    This changed allowed my watermark images with green in them to work properly.
    Last edited by cjbarth; 06-25-2010 at 03:55 PM.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  7. #7
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    Something else that I noticed when working with watermarking is the size of the watermarks. I found it tiresome to keep having to generate three different size watermarks every time I wanted to test a new watermark. To make things easier on me I made another code change to AddWatermark in CommonLogic.vb to automatically resize the watermark image.

    In the If...Then block that checks the CopyrightImageUrl.Length near the begining of the routine I replaced the contents with the following:

    Code:
    imgWatermark = New Bitmap(SafeMapPath(CopyrightImageUrl))
    
    Dim reductionFactor As Double = 1
    Dim heightRatio As Double = imgWatermark.Height / phHeight
    Dim widthRatio As Double = imgWatermark.Width / phWidth
    
    If heightRatio > widthRatio Then
    	reductionFactor = heightRatio
    Else
    	reductionFactor = widthRatio
    End If
    
    If reductionFactor < 1 Then
    	reductionFactor = 1
    End If
    
    wmWidth = CInt(imgWatermark.Width / reductionFactor)
    wmHeight = CInt(imgWatermark.Height / reductionFactor)
    
    If reductionFactor <> 1 Then
    	imgWatermark = New Bitmap(imgWatermark, wmWidth, wmHeight)
    End If
    Now my watermark image will automatically scale down, but never scale up.
    Last edited by cjbarth; 05-28-2010 at 07:22 AM.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM

  8. #8
    cjbarth is offline Senior Member
    Join Date
    Oct 2008
    Posts
    392

    Default

    While the above is for ML8, I've found the exact same situation exists with ML9 (at least with 9.0.1.3). The modifications above work with only a slight modification:

    Instead of modifying wolthuis.aspx.vb modify WatermarkHandler.vb.

    And when performing the modifications on any line that begins with Response.Foo you'll have to replace that with context.Response.Foo.

    Other than that everything is the same.
    ML9.3.1.1
    SQL 2012 Express
    VS 2010
    Azure VM