Imports System.Windows.Forms
Imports System.ComponentModel
Imports MediaCy.Addins.SCalibration
Imports MediaCy.Addins.SCalibration.Commands
Imports MediaCy.IQL.Calibrations

Public Class OpenImageTestApp


Public OpenFolderName As String
Private _init As Boolean = False

Private backwork As New System.ComponentModel.BackgroundWorker

    Private Sub MyControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyControl.Load
        backwork.WorkerSupportsCancellation = True
        backwork.WorkerReportsProgress = True
        ' Initialization code goes here
    End Sub

    Protected Overrides Sub Finalize()
        ' Cleanup code goes here
    End Sub

    Private Sub Initialize

        GetCalibrationName
        UpdateSC

    End Sub


    ' Provides access to underlying Control
    Public ReadOnly Property Control As System.Windows.Forms.UserControl
        Get
            Return MyControl
        End Get
    End Property
    Private Function backwork_DoWork
        For i As Integer = 0 To AllImageNumber-1


           Load_ImageName = AllImageName(i)
            Applyimage
            ImageNamelabel.Text = i.ToString
            Application.DoEvents()
            Do
                If IsStop Then
                    System.Threading.Thread.Sleep(0.5)
                    DoEvents
                    Application.DoEvents()
                Else
                    Exit Do
                End If
            Loop
            If backwork.CancellationPending Then
                Exit Function
            End If

        Next
    End Function


    Public Function classImage(ByVal s As String) As Boolean
        If s.tolower.endswith(".tif") = True Or s.ToLower.EndsWith("*.jpg") = True Or s.ToLower.EndsWith("*.bmp") = True Or s.ToLower.EndsWith("*.png") = True Then
            Return True
        Else
            Return False
        End If

    End Function


    Private Sub Choosebutton_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Choosebutton.Click
        Dim FolderBrowserDialog1 As FolderBrowserDialog = New FolderBrowserDialog
        FirstLoad = False
        AllImageNumber = 0
        If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
            OpenFolderName = FolderBrowserDialog1.SelectedPath
            FolderNamelabel.Text = OpenFolderName
            Dim FileTotalName() As String = system.Io.Directory.GetFiles(FolderBrowserDialog1.SelectedPath,"*",1)
            For i As Integer = 0 To FileTotalName.Length-1
                    Dim FileExtension As String = system.IO.Path.GetExtension(FileTotalName(i)).ToLower
                    If FileExtension = ".tif" Or FileExtension = ".jpg" Or FileExtension = ".bmp" Or FileExtension = ".png" Or FileExtension = ".bmp" Then
                        ReDim Preserve AllImageName(AllImageNumber)
                        AllImageName(AllImageNumber) = FileTotalName(i)
                        AllImageNumber += 1
                    End If
                Next
        End If
	End Sub

    Private Sub Bacthbutton_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Bacthbutton.Click
         FirstLoad = False
         IsStop = False

         If Calibration_combo.Text= ""   Then
            MsgBox("Please select calibration",,"Error")
                Exit Sub
         End If

        backwork.RunWorkerAsync(backwork_DoWork)

    End Sub

    Public Function OpenImageTest(ByVal filename As String) As SimpleScript
        OpenImageTest = New SimpleScript
        Dim img As McImage = Nothing
        img = ThisApplication.Images.Open(filename)
        img.Close
        img = Nothing
    End Function



    Private Sub button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles button2.Click
        backwork.CancelAsync
	End Sub


    Private Sub button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles button1.Click
        If IsStop Then
            IsStop = False
        Else
            IsStop = True
        End If

	End Sub


    Private Sub UpdateSC

        Calibration_combo.Items.Clear

        Try
            For ChangeCalibrationName As Integer = 0 To  CalibrationName.Length-1
                Calibration_combo.Items.Add(CalibrationName(ChangeCalibrationName))
            Next
        Catch
            Exit Sub
        End Try


     End Sub

     Private Sub Calibration_combo_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Calibration_combo.SelectedIndexChanged

        If zero_cali=True Then
            Exit Sub
        End If


        CalibrationSelected = Calibration_combo.SelectedItem.ToString
        SelectNumber = Calibration_combo.SelectedIndex
        CalibrationSelectTrue = True
        ApplyCalibration

    End Sub



    Private Sub Calibration_combo_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calibration_combo.DropDown
        GetCalibrationName
        If Not _init Then
            UpdateSC()
        End If

        If zero_cali=True Then
            Calibration_combo.Items.Clear
            Exit Sub
        End If
    End Sub



End Class
