Imports Stax
Imports Stax.UI
Imports Stax.AppFramework
Imports Stax.Win32

Public Class CropForm
    Inherits ExtendedForm

#Region " Vom Windows Form Designer generierter Code "
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Für Windows Form-Designer erforderlich
    Private components As System.ComponentModel.IContainer

    'HINWEIS: Die folgende Prozedur ist für den Windows Form-Designer erforderlich
    'Sie kann mit dem Windows Form-Designer modifiziert werden.
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    Friend WithEvents pbLeftActive As System.Windows.Forms.PictureBox
    Friend WithEvents pbTopActive As System.Windows.Forms.PictureBox
    Friend WithEvents pbBottomActive As System.Windows.Forms.PictureBox
    Friend WithEvents pbRightActive As System.Windows.Forms.PictureBox
    Friend WithEvents tbPosition As TrackBar
    Friend WithEvents pbVideo As System.Windows.Forms.PictureBox
    Friend WithEvents bClose As System.Windows.Forms.Button
    Friend WithEvents bTools As System.Windows.Forms.Button
    Friend WithEvents cmsMenu As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents HelpToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ToolStripItemExtender As Stax.UI.Design.ToolStripItemExtender
    Friend WithEvents ToolStripSeparator3 As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents ResizeToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents OriginalSizeToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents Resize43ToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents Resize169ToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents AutoToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents SmartToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents NavigateToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ForwardToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents BackwardToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents Increase2PixelToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents Decrease2PixelToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ToolStripSeparator1 As System.Windows.Forms.ToolStripSeparator
    Friend WithEvents ExitToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents StatusStrip As System.Windows.Forms.StatusStrip
    Friend WithEvents ToolStripStatusLabel As System.Windows.Forms.ToolStripStatusLabel
    Friend WithEvents ToolStripStatusLabel1 As System.Windows.Forms.ToolStripStatusLabel
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents CropToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.pbLeftActive = New System.Windows.Forms.PictureBox
        Me.pbTopActive = New System.Windows.Forms.PictureBox
        Me.pbBottomActive = New System.Windows.Forms.PictureBox
        Me.pbRightActive = New System.Windows.Forms.PictureBox
        Me.pbVideo = New System.Windows.Forms.PictureBox
        Me.cmsMenu = New System.Windows.Forms.ContextMenuStrip(Me.components)
        Me.CropToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.Increase2PixelToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.Decrease2PixelToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ToolStripSeparator1 = New System.Windows.Forms.ToolStripSeparator
        Me.AutoToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.SmartToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ResizeToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.OriginalSizeToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.Resize43ToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.Resize169ToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.NavigateToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ForwardToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.BackwardToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ExitToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ToolStripSeparator3 = New System.Windows.Forms.ToolStripSeparator
        Me.HelpToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.bClose = New System.Windows.Forms.Button
        Me.bTools = New System.Windows.Forms.Button
        Me.tbPosition = New System.Windows.Forms.TrackBar
        Me.ToolStripItemExtender = New Stax.UI.Design.ToolStripItemExtender
        Me.ToolStripStatusLabel1 = New System.Windows.Forms.ToolStripStatusLabel
        Me.StatusStrip = New System.Windows.Forms.StatusStrip
        Me.ToolStripStatusLabel = New System.Windows.Forms.ToolStripStatusLabel
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        CType(Me.pbLeftActive, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.pbTopActive, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.pbBottomActive, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.pbRightActive, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.pbVideo, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.cmsMenu.SuspendLayout()
        CType(Me.tbPosition, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.StatusStrip.SuspendLayout()
        Me.SuspendLayout()
        '
        'pbLeftActive
        '
        Me.pbLeftActive.BackColor = System.Drawing.SystemColors.InfoText
        Me.pbLeftActive.Location = New System.Drawing.Point(24, 56)
        Me.pbLeftActive.Name = "pbLeftActive"
        Me.pbLeftActive.Size = New System.Drawing.Size(8, 81)
        Me.pbLeftActive.TabIndex = 5
        Me.pbLeftActive.TabStop = False
        '
        'pbTopActive
        '
        Me.pbTopActive.BackColor = System.Drawing.SystemColors.InfoText
        Me.pbTopActive.Location = New System.Drawing.Point(64, 15)
        Me.pbTopActive.Name = "pbTopActive"
        Me.pbTopActive.Size = New System.Drawing.Size(152, 8)
        Me.pbTopActive.TabIndex = 6
        Me.pbTopActive.TabStop = False
        '
        'pbBottomActive
        '
        Me.pbBottomActive.BackColor = System.Drawing.SystemColors.InfoText
        Me.pbBottomActive.Location = New System.Drawing.Point(64, 184)
        Me.pbBottomActive.Name = "pbBottomActive"
        Me.pbBottomActive.Size = New System.Drawing.Size(152, 8)
        Me.pbBottomActive.TabIndex = 7
        Me.pbBottomActive.TabStop = False
        '
        'pbRightActive
        '
        Me.pbRightActive.BackColor = System.Drawing.SystemColors.InfoText
        Me.pbRightActive.Location = New System.Drawing.Point(256, 56)
        Me.pbRightActive.Name = "pbRightActive"
        Me.pbRightActive.Size = New System.Drawing.Size(8, 88)
        Me.pbRightActive.TabIndex = 8
        Me.pbRightActive.TabStop = False
        '
        'pbVideo
        '
        Me.pbVideo.BackColor = System.Drawing.Color.Black
        Me.pbVideo.ContextMenuStrip = Me.cmsMenu
        Me.pbVideo.Location = New System.Drawing.Point(64, 55)
        Me.pbVideo.Name = "pbVideo"
        Me.pbVideo.Size = New System.Drawing.Size(149, 89)
        Me.pbVideo.TabIndex = 9
        Me.pbVideo.TabStop = False
        '
        'cmsMenu
        '
        Me.cmsMenu.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.CropToolStripMenuItem, Me.ResizeToolStripMenuItem, Me.NavigateToolStripMenuItem, Me.ExitToolStripMenuItem, Me.ToolStripSeparator3, Me.HelpToolStripMenuItem})
        Me.cmsMenu.Name = "cmsMenu"
        Me.cmsMenu.Size = New System.Drawing.Size(129, 120)
        '
        'CropToolStripMenuItem
        '
        Me.CropToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.Increase2PixelToolStripMenuItem, Me.Decrease2PixelToolStripMenuItem, Me.ToolStripSeparator1, Me.AutoToolStripMenuItem, Me.SmartToolStripMenuItem})
        Me.CropToolStripMenuItem.Name = "CropToolStripMenuItem"
        Me.CropToolStripMenuItem.Size = New System.Drawing.Size(128, 22)
        Me.CropToolStripMenuItem.Text = "Crop"
        '
        'Increase2PixelToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.Increase2PixelToolStripMenuItem, System.Windows.Forms.Keys.Add)
        Me.Increase2PixelToolStripMenuItem.Name = "Increase2PixelToolStripMenuItem"
        Me.Increase2PixelToolStripMenuItem.ShortcutKeyDisplayString = ""
        Me.Increase2PixelToolStripMenuItem.Size = New System.Drawing.Size(164, 22)
        Me.Increase2PixelToolStripMenuItem.Text = "Increase 2 Pixel"
        Me.ToolStripItemExtender.SetTipText(Me.Increase2PixelToolStripMenuItem, "Increases the crop value of the selected side by 2 pixels.")
        '
        'Decrease2PixelToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.Decrease2PixelToolStripMenuItem, System.Windows.Forms.Keys.Subtract)
        Me.Decrease2PixelToolStripMenuItem.Name = "Decrease2PixelToolStripMenuItem"
        Me.Decrease2PixelToolStripMenuItem.ShortcutKeyDisplayString = ""
        Me.Decrease2PixelToolStripMenuItem.Size = New System.Drawing.Size(164, 22)
        Me.Decrease2PixelToolStripMenuItem.Text = "Decrease 2 Pixel"
        Me.ToolStripItemExtender.SetTipText(Me.Decrease2PixelToolStripMenuItem, "Decreases the crop value of the selected side by 2 pixels.")
        '
        'ToolStripSeparator1
        '
        Me.ToolStripSeparator1.Name = "ToolStripSeparator1"
        Me.ToolStripSeparator1.Size = New System.Drawing.Size(161, 6)
        '
        'AutoToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.AutoToolStripMenuItem, System.Windows.Forms.Keys.A)
        Me.AutoToolStripMenuItem.Name = "AutoToolStripMenuItem"
        Me.AutoToolStripMenuItem.Size = New System.Drawing.Size(164, 22)
        Me.AutoToolStripMenuItem.Text = "Auto Crop"
        Me.ToolStripItemExtender.SetTipText(Me.AutoToolStripMenuItem, "Detects the crop values automatically.")
        '
        'SmartToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.SmartToolStripMenuItem, System.Windows.Forms.Keys.S)
        Me.SmartToolStripMenuItem.Name = "SmartToolStripMenuItem"
        Me.SmartToolStripMenuItem.Size = New System.Drawing.Size(164, 22)
        Me.SmartToolStripMenuItem.Text = "Smart Crop"
        Me.ToolStripItemExtender.SetTipText(Me.SmartToolStripMenuItem, "Crops until the proper aspect ratio is found.")
        '
        'ResizeToolStripMenuItem
        '
        Me.ResizeToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.OriginalSizeToolStripMenuItem, Me.Resize43ToolStripMenuItem, Me.Resize169ToolStripMenuItem})
        Me.ResizeToolStripMenuItem.Name = "ResizeToolStripMenuItem"
        Me.ResizeToolStripMenuItem.Size = New System.Drawing.Size(128, 22)
        Me.ResizeToolStripMenuItem.Text = "Resize"
        '
        'OriginalSizeToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.OriginalSizeToolStripMenuItem, System.Windows.Forms.Keys.O)
        Me.OriginalSizeToolStripMenuItem.Name = "OriginalSizeToolStripMenuItem"
        Me.OriginalSizeToolStripMenuItem.Size = New System.Drawing.Size(121, 22)
        Me.OriginalSizeToolStripMenuItem.Text = "Original"
        Me.ToolStripItemExtender.SetTipText(Me.OriginalSizeToolStripMenuItem, "Resizes to the original size.")
        '
        'Resize43ToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.Resize43ToolStripMenuItem, System.Windows.Forms.Keys.F4)
        Me.Resize43ToolStripMenuItem.Name = "Resize43ToolStripMenuItem"
        Me.Resize43ToolStripMenuItem.Size = New System.Drawing.Size(121, 22)
        Me.Resize43ToolStripMenuItem.Text = "4:3"
        Me.ToolStripItemExtender.SetTipText(Me.Resize43ToolStripMenuItem, "Resizes the height to a aspect ratio of 4:3.")
        '
        'Resize169ToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.Resize169ToolStripMenuItem, System.Windows.Forms.Keys.F9)
        Me.Resize169ToolStripMenuItem.Name = "Resize169ToolStripMenuItem"
        Me.Resize169ToolStripMenuItem.Size = New System.Drawing.Size(121, 22)
        Me.Resize169ToolStripMenuItem.Text = "16:9"
        Me.ToolStripItemExtender.SetTipText(Me.Resize169ToolStripMenuItem, "Resizes the height to a aspect ratio of 16:9.")
        '
        'NavigateToolStripMenuItem
        '
        Me.NavigateToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ForwardToolStripMenuItem, Me.BackwardToolStripMenuItem})
        Me.NavigateToolStripMenuItem.Name = "NavigateToolStripMenuItem"
        Me.NavigateToolStripMenuItem.Size = New System.Drawing.Size(128, 22)
        Me.NavigateToolStripMenuItem.Text = "Navigate"
        '
        'ForwardToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.ForwardToolStripMenuItem, System.Windows.Forms.Keys.Space)
        Me.ForwardToolStripMenuItem.Name = "ForwardToolStripMenuItem"
        Me.ForwardToolStripMenuItem.Size = New System.Drawing.Size(131, 22)
        Me.ForwardToolStripMenuItem.Text = "Forward"
        '
        'BackwardToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.BackwardToolStripMenuItem, System.Windows.Forms.Keys.Back)
        Me.BackwardToolStripMenuItem.Name = "BackwardToolStripMenuItem"
        Me.BackwardToolStripMenuItem.Size = New System.Drawing.Size(131, 22)
        Me.BackwardToolStripMenuItem.Text = "Backward"
        '
        'ExitToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.ExitToolStripMenuItem, System.Windows.Forms.Keys.Escape)
        Me.ExitToolStripMenuItem.Name = "ExitToolStripMenuItem"
        Me.ExitToolStripMenuItem.Size = New System.Drawing.Size(128, 22)
        Me.ExitToolStripMenuItem.Text = "Exit"
        '
        'ToolStripSeparator3
        '
        Me.ToolStripSeparator3.Name = "ToolStripSeparator3"
        Me.ToolStripSeparator3.Size = New System.Drawing.Size(125, 6)
        '
        'HelpToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.HelpToolStripMenuItem, System.Windows.Forms.Keys.F1)
        Me.HelpToolStripMenuItem.Name = "HelpToolStripMenuItem"
        Me.HelpToolStripMenuItem.Size = New System.Drawing.Size(128, 22)
        Me.HelpToolStripMenuItem.Text = "Help"
        '
        'bClose
        '
        Me.bClose.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bClose.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bClose.Location = New System.Drawing.Point(347, 344)
        Me.bClose.Name = "bClose"
        Me.bClose.Size = New System.Drawing.Size(72, 24)
        Me.bClose.TabIndex = 2
        Me.bClose.Text = "Close"
        '
        'bTools
        '
        Me.bTools.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bTools.Location = New System.Drawing.Point(269, 344)
        Me.bTools.Name = "bTools"
        Me.bTools.Size = New System.Drawing.Size(72, 24)
        Me.bTools.TabIndex = 1
        Me.bTools.Text = "Tools"
        '
        'tbPosition
        '
        Me.tbPosition.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbPosition.AutoSize = False
        Me.tbPosition.Location = New System.Drawing.Point(4, 344)
        Me.tbPosition.Name = "tbPosition"
        Me.tbPosition.Size = New System.Drawing.Size(263, 24)
        Me.tbPosition.TabIndex = 0
        Me.tbPosition.TabStop = False
        Me.tbPosition.TickStyle = System.Windows.Forms.TickStyle.None
        '
        'ToolStripItemExtender
        '
        Me.ToolStripItemExtender.Owner = Me
        '
        'ToolStripStatusLabel1
        '
        Me.ToolStripStatusLabel1.AutoSize = False
        Me.ToolStripStatusLabel1.Name = "ToolStripStatusLabel1"
        Me.ToolStripStatusLabel1.Size = New System.Drawing.Size(200, 17)
        Me.ToolStripItemExtender.SetTipText(Me.ToolStripStatusLabel1, "The status panel displays the cropped size, the crop values and the aspect ratio " & _
                "error.")
        Me.ToolStripItemExtender.SetTipTitle(Me.ToolStripStatusLabel1, "Status Panel")
        '
        'StatusStrip
        '
        Me.StatusStrip.AutoSize = False
        Me.StatusStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripStatusLabel, Me.ToolStripStatusLabel1})
        Me.StatusStrip.Location = New System.Drawing.Point(0, 371)
        Me.StatusStrip.Name = "StatusStrip"
        Me.StatusStrip.Size = New System.Drawing.Size(431, 22)
        Me.StatusStrip.TabIndex = 10
        Me.StatusStrip.Text = "StatusStrip"
        '
        'ToolStripStatusLabel
        '
        Me.ToolStripStatusLabel.Name = "ToolStripStatusLabel"
        Me.ToolStripStatusLabel.Size = New System.Drawing.Size(0, 17)
        '
        'CropForm
        '
        Me.ClientSize = New System.Drawing.Size(431, 393)
        Me.Controls.Add(Me.StatusStrip)
        Me.Controls.Add(Me.bClose)
        Me.Controls.Add(Me.bTools)
        Me.Controls.Add(Me.tbPosition)
        Me.Controls.Add(Me.pbVideo)
        Me.Controls.Add(Me.pbRightActive)
        Me.Controls.Add(Me.pbBottomActive)
        Me.Controls.Add(Me.pbTopActive)
        Me.Controls.Add(Me.pbLeftActive)
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.MinimumSize = New System.Drawing.Size(200, 200)
        Me.Name = "CropForm"
        Me.ShowIcon = False
        Me.ShowInTaskbar = False
        Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "Crop"
        CType(Me.pbLeftActive, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.pbTopActive, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.pbBottomActive, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.pbRightActive, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.pbVideo, System.ComponentModel.ISupportInitialize).EndInit()
        Me.cmsMenu.ResumeLayout(False)
        CType(Me.tbPosition, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).EndInit()
        Me.StatusStrip.ResumeLayout(False)
        Me.StatusStrip.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public AVI As New AVIFile

    Private SelectedBorderColor As Color = Color.SkyBlue
    Private Side As AnchorStyles
    Private ActiveCropSide As AnchorStyles

    Public Sub New()
        MyBase.New()
        InitializeComponent()

        pbVideo.Left = 10
        pbVideo.Top = 10
        pbVideo.Width = ClientSize.Width - 20
        pbVideo.Height = ClientSize.Height - 72
        pbVideo.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom

        pbLeftActive.Top = 10
        pbLeftActive.Left = 0
        pbLeftActive.Width = 10
        pbLeftActive.Height = pbVideo.Height
        pbLeftActive.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Top

        pbTopActive.Left = 10
        pbTopActive.Top = 0
        pbTopActive.Width = pbVideo.Width
        pbTopActive.Height = 10
        pbTopActive.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right

        pbRightActive.Left = 10 + pbVideo.Width
        pbRightActive.Top = 10
        pbRightActive.Width = 10
        pbRightActive.Height = pbVideo.Height
        pbRightActive.Anchor = AnchorStyles.Top Or AnchorStyles.Right Or AnchorStyles.Bottom

        pbBottomActive.Left = 10
        pbBottomActive.Top = 10 + pbVideo.Height
        pbBottomActive.Width = pbVideo.Width
        pbBottomActive.Height = 10
        pbBottomActive.Anchor = AnchorStyles.Right Or AnchorStyles.Bottom Or AnchorStyles.Left

        SetSize(p.SourceWidth, p.SourceHeight)

        DeactivateActiveColor()

        If p.SourceFramerate <> 0 Then
            tbPosition.Maximum = CInt(p.SourceFrames / p.SourceFramerate)
        End If

        If tbPosition.Maximum < 1 Then
            tbPosition.Maximum = 1
        End If

        pbTopActive.BackColor = SelectedBorderColor
        Side = AnchorStyles.Top

        s.WindowPositions.Restore(Me, WindowPositions.Mode.Position)

        p.SourceAviSynthDocument.Synchronize()

        AVI.Open(p.SourceAviSynthDocument.Path, pbVideo)
        UpdateAll()
    End Sub

    Private Sub TrackLength_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles tbPosition.Scroll
        AVI.Position = CInt(AVI.FrameRate * tbPosition.Value)
        AVI.Draw()
    End Sub

    Private Sub DeactivateActiveColor()
        pbLeftActive.BackColor = Drawing.SystemColors.Control
        pbTopActive.BackColor = Drawing.SystemColors.Control
        pbRightActive.BackColor = Drawing.SystemColors.Control
        pbBottomActive.BackColor = Drawing.SystemColors.Control
    End Sub

    Private Sub CropForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        If Not s.StringList.Contains(Me.GetType.FullName) Then
            HelpToolStripMenuItem.PerformClick()
            s.StringList.Add(Me.GetType.FullName)
        End If
    End Sub

    Private Sub Wheel(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseWheel
        If e.Delta > 0 Then
            Change(2)
        Else
            Change(-2)
        End If
    End Sub

    Private Sub Change(ByVal x As Integer)
        Select Case Side
            Case AnchorStyles.Left
                p.CropLeft += x

                If p.CropLeft < 0 Then
                    p.CropLeft = 0
                End If
            Case AnchorStyles.Top
                p.CropTop += x

                If p.CropTop < 0 Then
                    p.CropTop = 0
                End If
            Case AnchorStyles.Right
                p.CropRight += x

                If p.CropRight < 0 Then
                    p.CropRight = 0
                End If
            Case AnchorStyles.Bottom
                p.CropBottom += x

                If p.CropBottom < 0 Then
                    p.CropBottom = 0
                End If
        End Select

        UpdateAll()
    End Sub

    Private Sub bCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bClose.Click
        Close()
    End Sub

    Private Sub bTools_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bTools.Click
        cmsMenu.Show(bTools, 0, bTools.Height)
    End Sub

    Private Function GetSide(ByVal e As MouseEventArgs) As AnchorStyles
        Dim factorX As Double = p.SourceWidth / pbVideo.Width
        Dim factorY As Double = p.SourceHeight / pbVideo.Height

        Dim leftSide As Integer = CInt(e.X * factorX)
        Dim topSide As Integer = CInt(e.Y * factorY)
        Dim rightSide As Integer = CInt((pbVideo.Width - e.X) * factorX)
        Dim bottomSide As Integer = CInt((pbVideo.Height - e.Y) * factorY)

        Dim sides As Integer() = {leftSide, topSide, rightSide, bottomSide}
        Array.Sort(sides)

        Select Case CInt(sides(0))
            Case leftSide
                Return AnchorStyles.Left
            Case topSide
                Return AnchorStyles.Top
            Case rightSide
                Return AnchorStyles.Right
            Case bottomSide
                Return AnchorStyles.Bottom
        End Select
    End Function

    Private Sub MouseCrop(ByVal e As MouseEventArgs)
        Dim factorX As Double = p.SourceWidth / pbVideo.Width
        Dim factorY As Double = p.SourceHeight / pbVideo.Height

        Dim leftSide As Integer = CInt(e.X * factorX)
        Dim topSide As Integer = CInt(e.Y * factorY)
        Dim rightSide As Integer = CInt((pbVideo.Width - e.X) * factorX)
        Dim bottomSide As Integer = CInt((pbVideo.Height - e.Y) * factorY)

        Select Case ActiveCropSide
            Case AnchorStyles.Left
                p.CropLeft = FixMod2(leftSide)

                If p.CropLeft < 0 Then
                    p.CropLeft = 0
                End If
            Case AnchorStyles.Top
                p.CropTop = FixMod2(topSide)

                If p.CropTop < 0 Then
                    p.CropTop = 0
                End If
            Case AnchorStyles.Right
                p.CropRight = FixMod2(rightSide)

                If p.CropRight < 0 Then
                    p.CropRight = 0
                End If
            Case AnchorStyles.Bottom
                p.CropBottom = FixMod2(bottomSide)

                If p.CropBottom < 0 Then
                    p.CropBottom = 0
                End If
        End Select

        UpdateAll()
    End Sub

    Private Sub MouseSelectBorder(ByVal e As MouseEventArgs)
        Select Case GetSide(e)
            Case AnchorStyles.Left
                DeactivateActiveColor()
                pbLeftActive.BackColor = SelectedBorderColor
                Side = AnchorStyles.Left
            Case AnchorStyles.Top
                DeactivateActiveColor()
                pbTopActive.BackColor = SelectedBorderColor
                Side = AnchorStyles.Top
            Case AnchorStyles.Right
                DeactivateActiveColor()
                pbRightActive.BackColor = SelectedBorderColor
                Side = AnchorStyles.Right
            Case AnchorStyles.Bottom
                DeactivateActiveColor()
                pbBottomActive.BackColor = SelectedBorderColor
                Side = AnchorStyles.Bottom
        End Select
    End Sub

    Private Function FixMod2(ByVal val As Integer) As Integer
        If Not val Mod 2 = 0 Then
            val += 1
        End If

        Return val
    End Function

    Sub UpdateAll()
        AVI.CropLeft = p.CropLeft
        AVI.CropTop = p.CropTop
        AVI.CropRight = p.CropRight
        AVI.CropBottom = p.CropBottom
        AVI.Draw()

        Dim croppedWidth As Integer = p.SourceWidth - p.CropLeft - p.CropRight
        Dim croppedHeight As Integer = p.SourceHeight - p.CropTop - p.CropBottom

        ToolStripStatusLabel.Text = croppedWidth.ToString() + "/" + croppedHeight.ToString() + "   " + p.CropLeft.ToString + "/" + p.CropTop.ToString + "/" + p.CropRight.ToString + "/" + p.CropBottom.ToString + "   " + g.MainForm.GetAspectRatioError().ToString("f2") + "%"
    End Sub

    Private Sub pbVideo_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles pbVideo.MouseMove
        If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
            MouseCrop(e)
        Else
            MouseSelectBorder(e)
        End If
    End Sub

    Private Sub pbVideo_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles pbVideo.MouseDown
        If Control.MouseButtons = Windows.Forms.MouseButtons.Left Then
            ActiveCropSide = GetSide(e)
            MouseCrop(e)
        End If
    End Sub

    Sub SetSize(ByVal w As Integer, ByVal h As Integer)
        ClientSize = New Size(ClientSize.Width + w - pbVideo.Width, _
            ClientSize.Height + h - pbVideo.Height)

        AVI.Draw()
    End Sub

    Private Sub pbVideo_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles pbVideo.Paint
        AVI.Draw(e.Graphics)
    End Sub

    Private Sub CropForm_SizeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.SizeChanged
        AVI.Draw()
    End Sub

    Private Sub OriginalSizeToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OriginalSizeToolStripMenuItem.Click
        SetSize(p.SourceWidth, p.SourceHeight)
    End Sub

    Private Sub Resize43ToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Resize43ToolStripMenuItem.Click
        SetSize(pbVideo.Width, CInt((pbVideo.Width / 4) * 3))
    End Sub

    Private Sub Resize169ToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Resize169ToolStripMenuItem.Click
        SetSize(pbVideo.Width, CInt((pbVideo.Width / 16) * 9))
    End Sub

    Private Sub AutoToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AutoToolStripMenuItem.Click
        g.AutoCrop()
        UpdateAll()
    End Sub

    Private Sub SmartToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SmartToolStripMenuItem.Click
        g.SmartCrop()
        UpdateAll()
    End Sub

    Private Sub bMenu_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles bTools.Paint
        ButtonHelp.DrawDropDownGlyph(bTools, e.Graphics, HorizontalAlignment.Right)
    End Sub

    Private Sub HelpToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles HelpToolStripMenuItem.Click
        Refresh()

        Dim f As New HelpForm()
        f.Document.WriteStart("Crop Dialog")
        f.Document.WriteParagraph("The crop dialog allows to crop borders. By default StaxRip detects the crop values automatically. It's recommended to check the detected values visually. A crop value can be changed roughly by moving the mouse while the left mouse button is pressed. Alternative methods are using the **mousewheel** or pressing + or - on the numpad.")
        f.Document.WriteTips(ToolStripItemExtender.GetTips)
        f.Document.WriteTable("Shortcut Keys", ToolStripItemExtender.GetKeys, False)
        f.Show()
    End Sub

    Private Sub Increase2PixelToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Increase2PixelToolStripMenuItem.Click
        Change(2)
    End Sub

    Private Sub Decrease2PixelToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Decrease2PixelToolStripMenuItem.Click
        Change(-2)
    End Sub

    Private Sub ForwardToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ForwardToolStripMenuItem.Click
        Dim val As Integer = tbPosition.Value
        val += CInt(tbPosition.Maximum / 20)

        If val > tbPosition.Maximum Then
            val = val - tbPosition.Maximum
        End If

        tbPosition.Value = val
        AVI.Position = CInt(AVI.FrameRate * val)
        AVI.Draw()
    End Sub

    Private Sub BackwardToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BackwardToolStripMenuItem.Click
        Dim val As Integer = tbPosition.Value
        val -= CInt(tbPosition.Maximum / 20)

        If val < 0 Then
            val = val + tbPosition.Maximum
        End If

        tbPosition.Value = val
        AVI.Position = CInt(AVI.FrameRate * val)
        AVI.Draw()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Close()
    End Sub

    Private Sub CropForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
        s.WindowPositions.Save(Me, WindowPositions.Mode.Position)
        FixCropMod()
        AVI.Close()
    End Sub

    Public Sub FixCropMod()
        Dim w As Integer = (p.SourceWidth - p.CropLeft - p.CropRight) Mod 4
        Dim h As Integer = (p.SourceHeight - p.CropTop - p.CropBottom) Mod 4

        If w <> 0 Then
            p.CropLeft += w
        End If

        If h <> 0 Then
            p.CropTop += h
        End If
    End Sub

    Private Sub tbPosition_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles tbPosition.Enter
        ActiveControl = Nothing
    End Sub

    Private Sub bMenu_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles bTools.Enter
        ActiveControl = Nothing
    End Sub
End Class