Imports Neodynamic.WebControls.ImageDraw

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            'Get total pages once
            Dim t As Integer = (DirectCast(Me.ImageDraw1.Elements(0), ImageElement)).MultiPageCount
            'Store it in the ViewState
            ViewState("TotalPages") = t

            'Load drop down list for pages
            Dim i As Integer
            For i = 0 To t - 1
                ddlPages.Items.Add(New ListItem("Page " + (i + 1).ToString(), i.ToString()))
            Next
            ddlPages.SelectedIndex = 0

            'Display first page
            Me.DisplayTiffPage(0)
        End If
    End Sub

    Private Sub DisplayTiffPage(ByVal increment As Integer)

        'get total pages
        Dim totalPages As Integer = CInt(ViewState("TotalPages"))

        'get ImageElement...
        Dim imgElem As ImageElement = DirectCast(Me.ImageDraw1.Elements(0), ImageElement)

        'get current page
        Dim cIndex As Integer = imgElem.MultiPageIndex + increment

        If (cIndex < 0) Then
            cIndex = 0
        ElseIf (cIndex > totalPages - 1) Then
            cIndex = totalPages - 1
        End If

        'Set page to display
        imgElem.MultiPageIndex = cIndex

        'Set output image name/id
        Me.ImageDraw1.OutputImageName = "Page" + cIndex.ToString()

        'Update drop down list index
        Me.ddlPages.SelectedIndex = cIndex

    End Sub


    Protected Sub prevButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles prevButton.Click
        Me.DisplayTiffPage(-1)
    End Sub

    Protected Sub nextButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles nextButton.Click
        Me.DisplayTiffPage(1)
    End Sub

    Protected Sub ddlPages_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPages.SelectedIndexChanged
        'get ImageElement...
        Dim imgElem As ImageElement = DirectCast(Me.ImageDraw1.Elements(0), ImageElement)
        'Set page to display
        imgElem.MultiPageIndex = Integer.Parse(Me.ddlPages.SelectedValue)
        'Set output image name/id
        Me.ImageDraw1.OutputImageName = "Page" + Me.ddlPages.SelectedValue
    End Sub



    Protected Sub ddlZoom_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlZoom.SelectedIndexChanged
        Me.SetZoom(Integer.Parse(Me.ddlZoom.SelectedValue))
    End Sub

    Private Sub SetZoom(ByVal zoom As Integer)
        'get ImageElement...
        Dim imgElem As ImageElement = DirectCast(Me.ImageDraw1.Elements(0), ImageElement)
        imgElem.Actions.Clear()

        If (zoom <> 100) Then
            'Create scale action
            Dim scaleAction As New Scale()
            scaleAction.WidthPercentage = zoom
            scaleAction.HeightPercentage = zoom

            imgElem.Actions.Add(scaleAction)
        End If

    End Sub

    Protected Sub chkWatermark_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkWatermark.CheckedChanged
        Me.Watermark(Me.chkWatermark.Checked)
    End Sub


    Private Sub Watermark(ByVal addWatermark As Boolean)
        If (addWatermark) Then
            'Create watermark
            Dim rectWatermark As New RectangleShapeElement()
            rectWatermark.Width = 370
            rectWatermark.Height = 70
            rectWatermark.Text = "CONFIDENTIAL"
            rectWatermark.Font.Bold = True
            rectWatermark.Font.Name = "Arial Narrow"
            rectWatermark.Font.Size = 30.0F
            rectWatermark.Fill.BackgroundColor = System.Drawing.Color.Transparent
            rectWatermark.StrokeWidth = 5
            rectWatermark.StrokeFill.BackgroundColor = System.Drawing.Color.FromArgb(127, System.Drawing.Color.Red)
            rectWatermark.TextForeColor = System.Drawing.Color.FromArgb(127, System.Drawing.Color.Red)


            'Rotate 45 degrees
            Dim rotAction As New Rotate()
            rotAction.Angle = 45

            'Apply rotation on watermark
            rectWatermark.Actions.Add(rotAction)

            'add watermark to composite image
            Me.ImageDraw1.Elements.Add(rectWatermark)
        Else
            'Remove watermark...
            If (Me.ImageDraw1.Elements.Count = 2) Then
                Me.ImageDraw1.Elements.RemoveAt(1)
            End If
        End If

    End Sub


End Class
