
Imports System
Imports System.Runtime.InteropServices
Imports System.IO

'Delgate for callback function
Public Delegate Function MyCallback(ByVal pBuf As IntPtr, ByVal lBufferSize As Integer, ByVal pCBContext As Integer) As Integer
Public Delegate Function MyCallbackEx(ByVal event_type As Integer, ByVal pBuf As IntPtr, ByVal lBufferSize As Integer, ByVal pCBContext As Integer) As Integer

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private cb As MyCallback    'If the callback is called asynchronously, you have to ensure that the
    'callback delegate is alive as long as the function poitner is needed.
    Private cb_ex As MyCallbackEx

    Dim g_iCurrentCam As Integer
    Dim g_iCurrentFormat As Integer
    Dim g_iBinSkipParam As Integer
    Dim g_iBinSkipMode As Integer
    Dim g_iTriggerMode As Integer
    Dim g_iTriggerDelay As Long
    Dim g_ModIndexCam(10) As Integer

    Public gSaveBmp As Boolean
    Public gSaveCallback As Boolean

    Dim roi_prop As ICubeApi.ROI_PROPERTY = New ICubeApi.ROI_PROPERTY

#Region " Vom Windows Form Designer generierter Code "

    Public Sub New()
        MyBase.New()

        ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
        InitializeComponent()

        ' Initialisierungen nach dem Aufruf InitializeComponent() hinzufügen

    End Sub

    ' Die Form überschreibt den Löschvorgang der Basisklasse, um Komponenten zu bereinigen.
    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 ButtonInit As System.Windows.Forms.Button
    Friend WithEvents ButtonOpen As System.Windows.Forms.Button
    Friend WithEvents ButtonClose As System.Windows.Forms.Button
    Friend WithEvents ButtonStart As System.Windows.Forms.Button
    Friend WithEvents ButtonStop As System.Windows.Forms.Button
    Friend WithEvents GroupBoxFlip As System.Windows.Forms.GroupBox
    Friend WithEvents CheckBoxFlipH As System.Windows.Forms.CheckBox
    Friend WithEvents CheckBoxFlipV As System.Windows.Forms.CheckBox
    Friend WithEvents GroupBoxGrab As System.Windows.Forms.GroupBox
    Friend WithEvents ButtonSaveBMP As System.Windows.Forms.Button
    Friend WithEvents ButtonSaveRaw As System.Windows.Forms.Button
    Friend WithEvents ComboBoxCam As System.Windows.Forms.ComboBox
    Friend WithEvents ComboBoxFormat As System.Windows.Forms.ComboBox
    Friend WithEvents GroupBoxTrigger As System.Windows.Forms.GroupBox
    Friend WithEvents CheckBoxEnableTrigger As System.Windows.Forms.CheckBox
    Friend WithEvents RadioButtonSWTrigger As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButtonHWTrigger As System.Windows.Forms.RadioButton
    Friend WithEvents ButtonDoSWTrig As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TrackBarTriggerDelay As System.Windows.Forms.TrackBar
    Friend WithEvents TextBoxTriggerDelay As System.Windows.Forms.TextBox
    Friend WithEvents ButtonSetDelay As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents LabelApiVersion As System.Windows.Forms.Label
    Friend WithEvents ButtonSaveAVI As System.Windows.Forms.Button
    Friend WithEvents ButtonSaveCallbackToBMP As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.ButtonInit = New System.Windows.Forms.Button()
        Me.ButtonOpen = New System.Windows.Forms.Button()
        Me.ButtonClose = New System.Windows.Forms.Button()
        Me.ButtonStart = New System.Windows.Forms.Button()
        Me.ButtonStop = New System.Windows.Forms.Button()
        Me.GroupBoxFlip = New System.Windows.Forms.GroupBox()
        Me.CheckBoxFlipV = New System.Windows.Forms.CheckBox()
        Me.CheckBoxFlipH = New System.Windows.Forms.CheckBox()
        Me.GroupBoxGrab = New System.Windows.Forms.GroupBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.ButtonSaveCallbackToBMP = New System.Windows.Forms.Button()
        Me.ButtonSaveAVI = New System.Windows.Forms.Button()
        Me.ButtonSaveRaw = New System.Windows.Forms.Button()
        Me.ButtonSaveBMP = New System.Windows.Forms.Button()
        Me.ComboBoxCam = New System.Windows.Forms.ComboBox()
        Me.ComboBoxFormat = New System.Windows.Forms.ComboBox()
        Me.GroupBoxTrigger = New System.Windows.Forms.GroupBox()
        Me.ButtonSetDelay = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.ButtonDoSWTrig = New System.Windows.Forms.Button()
        Me.RadioButtonHWTrigger = New System.Windows.Forms.RadioButton()
        Me.RadioButtonSWTrigger = New System.Windows.Forms.RadioButton()
        Me.CheckBoxEnableTrigger = New System.Windows.Forms.CheckBox()
        Me.TrackBarTriggerDelay = New System.Windows.Forms.TrackBar()
        Me.TextBoxTriggerDelay = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.LabelApiVersion = New System.Windows.Forms.Label()
        Me.GroupBoxFlip.SuspendLayout()
        Me.GroupBoxGrab.SuspendLayout()
        Me.GroupBoxTrigger.SuspendLayout()
        CType(Me.TrackBarTriggerDelay, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'ButtonInit
        '
        Me.ButtonInit.Location = New System.Drawing.Point(29, 55)
        Me.ButtonInit.Name = "ButtonInit"
        Me.ButtonInit.Size = New System.Drawing.Size(90, 27)
        Me.ButtonInit.TabIndex = 0
        Me.ButtonInit.Text = "&Init"
        '
        'ButtonOpen
        '
        Me.ButtonOpen.Location = New System.Drawing.Point(29, 92)
        Me.ButtonOpen.Name = "ButtonOpen"
        Me.ButtonOpen.Size = New System.Drawing.Size(90, 27)
        Me.ButtonOpen.TabIndex = 1
        Me.ButtonOpen.Text = "&Open"
        '
        'ButtonClose
        '
        Me.ButtonClose.Location = New System.Drawing.Point(29, 129)
        Me.ButtonClose.Name = "ButtonClose"
        Me.ButtonClose.Size = New System.Drawing.Size(90, 27)
        Me.ButtonClose.TabIndex = 2
        Me.ButtonClose.Text = "&Close"
        '
        'ButtonStart
        '
        Me.ButtonStart.Location = New System.Drawing.Point(29, 166)
        Me.ButtonStart.Name = "ButtonStart"
        Me.ButtonStart.Size = New System.Drawing.Size(90, 27)
        Me.ButtonStart.TabIndex = 3
        Me.ButtonStart.Text = "&Start"
        '
        'ButtonStop
        '
        Me.ButtonStop.Location = New System.Drawing.Point(29, 203)
        Me.ButtonStop.Name = "ButtonStop"
        Me.ButtonStop.Size = New System.Drawing.Size(90, 27)
        Me.ButtonStop.TabIndex = 4
        Me.ButtonStop.Text = "Sto&p"
        '
        'GroupBoxFlip
        '
        Me.GroupBoxFlip.Controls.Add(Me.CheckBoxFlipV)
        Me.GroupBoxFlip.Controls.Add(Me.CheckBoxFlipH)
        Me.GroupBoxFlip.Location = New System.Drawing.Point(29, 249)
        Me.GroupBoxFlip.Name = "GroupBoxFlip"
        Me.GroupBoxFlip.Size = New System.Drawing.Size(240, 56)
        Me.GroupBoxFlip.TabIndex = 5
        Me.GroupBoxFlip.TabStop = False
        Me.GroupBoxFlip.Text = "Flip:"
        '
        'CheckBoxFlipV
        '
        Me.CheckBoxFlipV.Location = New System.Drawing.Point(125, 18)
        Me.CheckBoxFlipV.Name = "CheckBoxFlipV"
        Me.CheckBoxFlipV.Size = New System.Drawing.Size(86, 28)
        Me.CheckBoxFlipV.TabIndex = 1
        Me.CheckBoxFlipV.Text = "vertical"
        '
        'CheckBoxFlipH
        '
        Me.CheckBoxFlipH.Location = New System.Drawing.Point(10, 18)
        Me.CheckBoxFlipH.Name = "CheckBoxFlipH"
        Me.CheckBoxFlipH.Size = New System.Drawing.Size(86, 28)
        Me.CheckBoxFlipH.TabIndex = 0
        Me.CheckBoxFlipH.Text = "horizontal"
        '
        'GroupBoxGrab
        '
        Me.GroupBoxGrab.Controls.Add(Me.Label3)
        Me.GroupBoxGrab.Controls.Add(Me.ButtonSaveCallbackToBMP)
        Me.GroupBoxGrab.Controls.Add(Me.ButtonSaveAVI)
        Me.GroupBoxGrab.Controls.Add(Me.ButtonSaveRaw)
        Me.GroupBoxGrab.Controls.Add(Me.ButtonSaveBMP)
        Me.GroupBoxGrab.Location = New System.Drawing.Point(298, 360)
        Me.GroupBoxGrab.Name = "GroupBoxGrab"
        Me.GroupBoxGrab.Size = New System.Drawing.Size(288, 120)
        Me.GroupBoxGrab.TabIndex = 6
        Me.GroupBoxGrab.TabStop = False
        Me.GroupBoxGrab.Text = "Grab:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(154, 18)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(124, 19)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "Grab Callback data:"
        '
        'ButtonSaveCallbackToBMP
        '
        Me.ButtonSaveCallbackToBMP.Location = New System.Drawing.Point(154, 83)
        Me.ButtonSaveCallbackToBMP.Name = "ButtonSaveCallbackToBMP"
        Me.ButtonSaveCallbackToBMP.Size = New System.Drawing.Size(115, 27)
        Me.ButtonSaveCallbackToBMP.TabIndex = 3
        Me.ButtonSaveCallbackToBMP.Text = "Save to BMP"
        '
        'ButtonSaveAVI
        '
        Me.ButtonSaveAVI.Location = New System.Drawing.Point(19, 83)
        Me.ButtonSaveAVI.Name = "ButtonSaveAVI"
        Me.ButtonSaveAVI.Size = New System.Drawing.Size(90, 27)
        Me.ButtonSaveAVI.TabIndex = 2
        Me.ButtonSaveAVI.Text = "Save &AVI"
        '
        'ButtonSaveRaw
        '
        Me.ButtonSaveRaw.Location = New System.Drawing.Point(154, 46)
        Me.ButtonSaveRaw.Name = "ButtonSaveRaw"
        Me.ButtonSaveRaw.Size = New System.Drawing.Size(115, 27)
        Me.ButtonSaveRaw.TabIndex = 1
        Me.ButtonSaveRaw.Text = "Save to RAW"
        '
        'ButtonSaveBMP
        '
        Me.ButtonSaveBMP.Location = New System.Drawing.Point(19, 46)
        Me.ButtonSaveBMP.Name = "ButtonSaveBMP"
        Me.ButtonSaveBMP.Size = New System.Drawing.Size(90, 27)
        Me.ButtonSaveBMP.TabIndex = 0
        Me.ButtonSaveBMP.Text = "Save &BMP"
        '
        'ComboBoxCam
        '
        Me.ComboBoxCam.Location = New System.Drawing.Point(134, 55)
        Me.ComboBoxCam.Name = "ComboBoxCam"
        Me.ComboBoxCam.Size = New System.Drawing.Size(146, 24)
        Me.ComboBoxCam.TabIndex = 7
        '
        'ComboBoxFormat
        '
        Me.ComboBoxFormat.Location = New System.Drawing.Point(134, 92)
        Me.ComboBoxFormat.Name = "ComboBoxFormat"
        Me.ComboBoxFormat.Size = New System.Drawing.Size(146, 24)
        Me.ComboBoxFormat.TabIndex = 8
        '
        'GroupBoxTrigger
        '
        Me.GroupBoxTrigger.Controls.Add(Me.ButtonSetDelay)
        Me.GroupBoxTrigger.Controls.Add(Me.Label1)
        Me.GroupBoxTrigger.Controls.Add(Me.ButtonDoSWTrig)
        Me.GroupBoxTrigger.Controls.Add(Me.RadioButtonHWTrigger)
        Me.GroupBoxTrigger.Controls.Add(Me.RadioButtonSWTrigger)
        Me.GroupBoxTrigger.Controls.Add(Me.CheckBoxEnableTrigger)
        Me.GroupBoxTrigger.Controls.Add(Me.TrackBarTriggerDelay)
        Me.GroupBoxTrigger.Controls.Add(Me.TextBoxTriggerDelay)
        Me.GroupBoxTrigger.Location = New System.Drawing.Point(298, 55)
        Me.GroupBoxTrigger.Name = "GroupBoxTrigger"
        Me.GroupBoxTrigger.Size = New System.Drawing.Size(288, 296)
        Me.GroupBoxTrigger.TabIndex = 9
        Me.GroupBoxTrigger.TabStop = False
        Me.GroupBoxTrigger.Text = "Trigger Control"
        '
        'ButtonSetDelay
        '
        Me.ButtonSetDelay.Location = New System.Drawing.Point(173, 258)
        Me.ButtonSetDelay.Name = "ButtonSetDelay"
        Me.ButtonSetDelay.Size = New System.Drawing.Size(90, 27)
        Me.ButtonSetDelay.TabIndex = 102
        Me.ButtonSetDelay.Text = "Set Delay"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(19, 157)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 18)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "TriggerDelay:"
        '
        'ButtonDoSWTrig
        '
        Me.ButtonDoSWTrig.Location = New System.Drawing.Point(173, 129)
        Me.ButtonDoSWTrig.Name = "ButtonDoSWTrig"
        Me.ButtonDoSWTrig.Size = New System.Drawing.Size(90, 27)
        Me.ButtonDoSWTrig.TabIndex = 3
        Me.ButtonDoSWTrig.Text = "Do SW Trig"
        '
        'RadioButtonHWTrigger
        '
        Me.RadioButtonHWTrigger.Location = New System.Drawing.Point(29, 92)
        Me.RadioButtonHWTrigger.Name = "RadioButtonHWTrigger"
        Me.RadioButtonHWTrigger.Size = New System.Drawing.Size(134, 28)
        Me.RadioButtonHWTrigger.TabIndex = 2
        Me.RadioButtonHWTrigger.Text = "Hardware Trigger"
        '
        'RadioButtonSWTrigger
        '
        Me.RadioButtonSWTrigger.Checked = True
        Me.RadioButtonSWTrigger.Location = New System.Drawing.Point(29, 65)
        Me.RadioButtonSWTrigger.Name = "RadioButtonSWTrigger"
        Me.RadioButtonSWTrigger.Size = New System.Drawing.Size(134, 27)
        Me.RadioButtonSWTrigger.TabIndex = 1
        Me.RadioButtonSWTrigger.TabStop = True
        Me.RadioButtonSWTrigger.Text = "Software Trigger"
        '
        'CheckBoxEnableTrigger
        '
        Me.CheckBoxEnableTrigger.Location = New System.Drawing.Point(29, 28)
        Me.CheckBoxEnableTrigger.Name = "CheckBoxEnableTrigger"
        Me.CheckBoxEnableTrigger.Size = New System.Drawing.Size(173, 27)
        Me.CheckBoxEnableTrigger.TabIndex = 0
        Me.CheckBoxEnableTrigger.Text = "enable Trigger mode"
        '
        'TrackBarTriggerDelay
        '
        Me.TrackBarTriggerDelay.Location = New System.Drawing.Point(19, 194)
        Me.TrackBarTriggerDelay.Name = "TrackBarTriggerDelay"
        Me.TrackBarTriggerDelay.Size = New System.Drawing.Size(192, 56)
        Me.TrackBarTriggerDelay.TabIndex = 100
        '
        'TextBoxTriggerDelay
        '
        Me.TextBoxTriggerDelay.Location = New System.Drawing.Point(221, 194)
        Me.TextBoxTriggerDelay.Name = "TextBoxTriggerDelay"
        Me.TextBoxTriggerDelay.Size = New System.Drawing.Size(48, 22)
        Me.TextBoxTriggerDelay.TabIndex = 101
        Me.TextBoxTriggerDelay.Text = "0"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(38, 18)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(87, 19)
        Me.Label2.TabIndex = 10
        Me.Label2.Text = "API Version:"
        '
        'LabelApiVersion
        '
        Me.LabelApiVersion.Location = New System.Drawing.Point(144, 18)
        Me.LabelApiVersion.Name = "LabelApiVersion"
        Me.LabelApiVersion.Size = New System.Drawing.Size(86, 19)
        Me.LabelApiVersion.TabIndex = 11
        Me.LabelApiVersion.Text = "0.0.0.0"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(628, 510)
        Me.Controls.Add(Me.LabelApiVersion)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.GroupBoxTrigger)
        Me.Controls.Add(Me.ComboBoxFormat)
        Me.Controls.Add(Me.ComboBoxCam)
        Me.Controls.Add(Me.GroupBoxGrab)
        Me.Controls.Add(Me.GroupBoxFlip)
        Me.Controls.Add(Me.ButtonStop)
        Me.Controls.Add(Me.ButtonStart)
        Me.Controls.Add(Me.ButtonClose)
        Me.Controls.Add(Me.ButtonOpen)
        Me.Controls.Add(Me.ButtonInit)
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Form1"
        Me.Text = "ICubeSDKSample 1.0.0.9"
        Me.GroupBoxFlip.ResumeLayout(False)
        Me.GroupBoxGrab.ResumeLayout(False)
        Me.GroupBoxTrigger.ResumeLayout(False)
        Me.GroupBoxTrigger.PerformLayout()
        CType(Me.TrackBarTriggerDelay, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            Dim Buffer As String
            Buffer = New String(CChar(" "), ICubeApi.NETCAM_VERSION_LENGTH)
            ICubeApi.ICubeSDK_GetVersion(ICubeApi.API_VERSION, Buffer)
            LabelApiVersion.Text = Buffer
        Catch
            MessageBox.Show("No ICubeSDK.dll available", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Application.Exit()
        End Try

        g_iCurrentCam = 0
        g_iCurrentFormat = 0
        g_iBinSkipParam = ICubeApi.BIN_SKIP_OFF  'set the bin/skip mode if available
        g_iBinSkipMode = ICubeApi.MODE_BIN       'set binning or skipping mode
        g_iTriggerMode = ICubeApi.TRIG_SW_START
        g_iTriggerDelay = 0

        roi_prop.bEnabled = 0   ' set to 1 to enable roi mode
        roi_prop.nXPos = 0
        roi_prop.nYPos = 0
        roi_prop.nXRes = 640
        roi_prop.nYRes = 480

    End Sub
    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

        Call CloseCam()
        GC.Collect()                     'free unmanaged memory -> dll

    End Sub
    Private Function GetModString(ByVal nFormat As Integer) As String

        Select Case nFormat
            Case ICubeApi.MODE_320x240
                Return "320x240"
            Case ICubeApi.MODE_640x480
                Return "640x480"
            Case ICubeApi.MODE_752x480
                Return "752x480"
            Case ICubeApi.MODE_800x600
                Return "800x600"
            Case ICubeApi.MODE_1024x768
                Return "1024x768"
            Case ICubeApi.MODE_1280x1024
                Return "1280x1024"
            Case ICubeApi.MODE_1600x1200
                Return "1600x1200"
            Case ICubeApi.MODE_2048x1536
                Return "2048x1536"
            Case ICubeApi.MODE_2592x1944
                Return "2592x1944"
            Case ICubeApi.MODE_3840x2748
                Return "3840x2748"
            Case ICubeApi.MODE_1920x1200
                Return "1920x1200"
            Case Else
                Return Nothing
        End Select
    End Function
    Private Sub ButtonInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonInit.Click

        Dim nSize As Integer
        Dim i As Integer
        Dim stat As Integer
        Dim Buffer As String
        Dim NumOfCam As Integer         'Number of connected cameras
   
        nSize = ICubeApi.NETCAM_NAME_LENGTH
        Buffer = New String(CChar(" "), nSize)

        NumOfCam = ICubeApi.ICubeSDK_Init()
        If NumOfCam = -1 Then
            MessageBox.Show("Error Init", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        ElseIf NumOfCam < 1 Then
            MessageBox.Show("No Camera connected", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        ComboBoxCam.Items.Clear()

        For i = 0 To NumOfCam - 1
            stat = ICubeApi.ICubeSDK_GetName(i, Buffer)
            If stat = ICubeApi.IC_ERROR Then
                MessageBox.Show("Error GetName", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Return
            ElseIf stat = ICubeApi.IC_DRIVER_VERSION Then
                MessageBox.Show("Error driver version mismatch", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Return
            End If
            ComboBoxCam.Items.Add(Buffer)
        Next

        ComboBoxCam.SelectedIndex = 0       'Selected Cam == first cam
        g_iCurrentCam = 0

    End Sub

    Private Sub ButtonOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click
        Dim nStat As Integer
        Dim nlist As Integer
        Dim i As Integer

        nlist = 10

        g_iCurrentCam = ComboBoxCam.SelectedIndex           ' get the Camera to open

        nStat = ICubeApi.ICubeSDK_Open(g_iCurrentCam)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error Open", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        Call InitTriggerDelay()

        nStat = ICubeApi.ICubeSDK_GetModeList(g_iCurrentCam, nlist, g_ModIndexCam)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error GetModeList", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        ComboBoxFormat.Items.Clear()

        For i = 0 To nlist - 1
            ComboBoxFormat.Items.Add(GetModString(g_ModIndexCam(i)))
        Next

        nStat = ICubeApi.ICubeSDK_GetMode(g_iCurrentCam, g_iCurrentFormat)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error GetModeList", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        For i = 0 To nlist - 1
            If g_ModIndexCam(i) = g_iCurrentFormat Then
                ComboBoxFormat.SelectedIndex = i
                Return
            End If
        Next

    End Sub
    Private Sub ComboBoxFormat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxFormat.SelectedIndexChanged

        g_iCurrentFormat = g_ModIndexCam(ComboBoxFormat.SelectedIndex)

    End Sub

    Private Sub ButtonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClose.Click

        Call CloseCam()

    End Sub

    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click

        Call StartCam()

    End Sub

    Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click

        If Not ICubeApi.ICubeSDK_Stop(g_iCurrentCam) = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error Stop", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub
    Private Sub CloseCam()

        Dim nStat As Integer

        nStat = ICubeApi.ICubeSDK_IsOpen(g_iCurrentCam)
        If nStat = ICubeApi.ON_ Then
            If Not ICubeApi.ICubeSDK_Close(g_iCurrentCam) = ICubeApi.IC_SUCCESS Then
                MessageBox.Show("Error Close", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If

    End Sub
    ''---------------------------------------------------------------------
    ''
    '' ICubeSDK_SetMode() sets the basis format (e.g. 640x480)
    '' ICubeSDK_SetBinSkip() sets Binning/Skipping on the basis format.
    '' If ICubeSDK_SetBinSkip->nParameter==0, ICubeSDK_SetBinSkip has
    '' no effect.
    '' ICubeSDK_SetResolution sets the Region of interest.
    '' If roi_prop.bEnabled==0,ICubeSDK_SetResolution has
    '' no effect.  
    '' It is not possible to set bin/skip and Roi.
    '' 
    Private Sub StartCam()

        Dim nStat As Integer

        ' optional to set format, different from highest format
        nStat = ICubeApi.ICubeSDK_SetMode(g_iCurrentCam, g_iCurrentFormat)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetMode", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        ' optional to set bin skip mode
        nStat = ICubeApi.ICubeSDK_SetBinSkip(g_iCurrentCam, g_iBinSkipParam, g_iBinSkipMode)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetBinSkip", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        ' optional to set region of interest mode
        nStat = ICubeApi.ICubeSDK_SetResolution(g_iCurrentCam, roi_prop)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetResolution", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        ' necessary, if Callback-Flag==true 
        cb_ex = AddressOf MyCallbackFuncEx
        nStat = ICubeApi.ICubeSDK_SetCallbackEx(g_iCurrentCam, ICubeApi.CALLBACK_RGB, cb_ex, Nothing)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetCallback", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        nStat = ICubeApi.ICubeSDK_Start(g_iCurrentCam, Nothing, True, True)
        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error Start", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub
    ' CallbackEx Function
    Public Function MyCallbackFuncEx(ByVal event_type As Integer, ByVal pBuf As IntPtr, ByVal lBufferSize As Integer, ByVal pCBContext As Integer) As Integer

        If event_type = ICubeApi.EVENT_NEW_FRAME Then
            If gSaveCallback = True Then     ' save data from callback
                gSaveCallback = False

                Dim buf(lBufferSize) As Byte
                Marshal.Copy(pBuf, buf, 0, lBufferSize)

                If gSaveBmp = False Then

                    Dim aFileStream As FileStream
                    Try
                        aFileStream = New FileStream("test.raw", FileMode.OpenOrCreate, FileAccess.Write)
                        Dim myBinaryWriter As New BinaryWriter(aFileStream)
                        myBinaryWriter.Write(buf)
                    Catch e As Exception
                        Console.WriteLine(e.StackTrace)
                    Finally
                        If Not (aFileStream Is Nothing) Then aFileStream.Close()
                    End Try

                Else
                    Try
                        Dim nHeight As Integer
                        Dim nWidth As Integer

                        ICubeApi.ICubeSDK_GetSize(g_iCurrentCam, nHeight, nWidth)
                        Dim bmp = New Bitmap(nHeight, nWidth)
                        Dim bData As Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, nHeight, nWidth), _
                                Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format24bppRgb)
                        Marshal.Copy(buf, 0, bData.Scan0, lBufferSize) ' copy to bitmap
                        bmp.UnlockBits(bData)
                        bmp.Save("test.bmp", Imaging.ImageFormat.Bmp)
                    Catch e As Exception
                        Console.WriteLine(e.StackTrace)
                    End Try
                End If

            End If

        End If

        Return 1
    End Function
    ' Callback Function

    Public Function MyCallbackFunc(ByVal pBuf As IntPtr, ByVal lBufferSize As Integer, ByVal pCBContext As Integer) As Integer

        If gSaveCallback = True Then     ' save data from callback
            gSaveCallback = False

            Dim buf(lBufferSize) As Byte
            Marshal.Copy(pBuf, buf, 0, lBufferSize)

            If gSaveBmp = False Then

            Dim aFileStream As FileStream
            Try
                aFileStream = New FileStream("test.raw", FileMode.OpenOrCreate, FileAccess.Write)
                Dim myBinaryWriter As New BinaryWriter(aFileStream)
                myBinaryWriter.Write(buf)
            Catch e As Exception
                Console.WriteLine(e.StackTrace)
            Finally
                If Not (aFileStream Is Nothing) Then aFileStream.Close()
                End Try

            Else
                Try
                    Dim nHeight As Integer
                    Dim nWidth As Integer

                    ICubeApi.ICubeSDK_GetSize(g_iCurrentCam, nHeight, nWidth)
                    Dim bmp = New Bitmap(nHeight, nWidth)
                    Dim bData As Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, nHeight, nWidth), _
                            Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format24bppRgb)
                    Marshal.Copy(buf, 0, bData.Scan0, lBufferSize) ' copy to bitmap
                    bmp.UnlockBits(bData)
                    bmp.Save("test.bmp", Imaging.ImageFormat.Bmp)
                Catch e As Exception
                    Console.WriteLine(e.StackTrace)
                End Try
            End If

        End If

        Return 1
    End Function

    ' Save Controls

    Private Sub ButtonSaveBMP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveBMP.Click

        If Not ICubeApi.ICubeSDK_SaveToFile(g_iCurrentCam, "123456789.bmp") = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SaveToFile", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    Private Sub ButtonSaveRaw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveRaw.Click

        gSaveBmp = False        ' save callback data to a raw-File
        gSaveCallback = True

    End Sub

    ' Flip Controls

    Private Sub CheckBoxFlipH_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxFlipH.CheckedChanged

        Dim nFlipMode As Integer

        If CheckBoxFlipH.Checked = True Then
            nFlipMode = ICubeApi.ON_
        Else
            nFlipMode = ICubeApi.OFF
        End If

        If Not ICubeApi.ICubeSDK_SetCamParameter(g_iCurrentCam, ICubeApi.REG_FLIPPED_H, nFlipMode) = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetCamParameter", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    Private Sub CheckBoxFlipV_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxFlipV.CheckedChanged

        Dim nFlipMode As Integer

        If CheckBoxFlipV.Checked = True Then
            nFlipMode = ICubeApi.ON_
        Else
            nFlipMode = ICubeApi.OFF
        End If

        If Not ICubeApi.ICubeSDK_SetCamParameter(g_iCurrentCam, ICubeApi.REG_FLIPPED_V, nFlipMode) = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetCamParameter", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    ' Trigger Controls

    Private Sub CheckBoxEnableTrigger_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxEnableTrigger.CheckedChanged

        Dim nStat As Integer

        If CheckBoxEnableTrigger.Checked = True Then
            nStat = ICubeApi.ICubeSDK_SetTrigger(g_iCurrentCam, g_iTriggerMode)
        Else
            nStat = ICubeApi.ICubeSDK_SetTrigger(g_iCurrentCam, ICubeApi.TRIG_STOP)
        End If

        If Not nStat = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetTrigger", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    Private Sub RadioButtonSWTrigger_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonSWTrigger.CheckedChanged

        g_iTriggerMode = ICubeApi.TRIG_SW_START

    End Sub

    Private Sub RadioButtonHWTrigger_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonHWTrigger.CheckedChanged

        g_iTriggerMode = ICubeApi.TRIG_HW_START

    End Sub

    Private Sub ButtonDoSWTrig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDoSWTrig.Click

        If g_iTriggerMode = ICubeApi.TRIG_HW_START Then
            MessageBox.Show("Wrong Trigger mode!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return
        End If

        If CheckBoxEnableTrigger.Checked = True Then
            If Not ICubeApi.ICubeSDK_SetTrigger(g_iCurrentCam, ICubeApi.TRIG_SW_DO) = ICubeApi.IC_SUCCESS Then
                MessageBox.Show("Error SetTrigger", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Else
                MessageBox.Show("Enable Trigger mode first!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If

    End Sub

    Private Sub ButtonSetDelay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSetDelay.Click

        If Not ICubeApi.ICubeSDK_SetCamParameter(g_iCurrentCam, ICubeApi.REG_TRIGGER_DELAY, g_iTriggerDelay) = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error SetCamParameter", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    Private Sub TrackBarTriggerDelay_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBarTriggerDelay.Scroll

        TextBoxTriggerDelay.Text = TrackBarTriggerDelay.Value.ToString
        g_iTriggerDelay = TrackBarTriggerDelay.Value

    End Sub

    Private Sub InitTriggerDelay()

        Dim param_prop As ICubeApi.PARAM_PROPERTY = New ICubeApi.PARAM_PROPERTY

        If Not ICubeApi.ICubeSDK_GetCamParameterRange(g_iCurrentCam, ICubeApi.REG_TRIGGER_DELAY, param_prop) = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error GetCamParameterRange", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

        If param_prop.bEnabled Then
            If Not ICubeApi.ICubeSDK_GetCamParameter(g_iCurrentCam, ICubeApi.REG_TRIGGER_DELAY, g_iTriggerDelay) = ICubeApi.IC_SUCCESS Then
                MessageBox.Show("Error GetCamParameter", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If

            TrackBarTriggerDelay.Maximum = param_prop.nMax
            TrackBarTriggerDelay.Minimum = param_prop.nMin
            TrackBarTriggerDelay.Value = g_iTriggerDelay
            TextBoxTriggerDelay.Text = g_iTriggerDelay.ToString

        End If
    End Sub

    Private Sub ButtonSaveAVI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveAVI.Click

        Dim nTimeInSeconds As Integer

        nTimeInSeconds = 1

        If Not ICubeApi.ICubeSDK_SaveAvi(g_iCurrentCam, "VBnet.avi", ICubeApi.AVI_DIB, nTimeInSeconds) = ICubeApi.IC_SUCCESS Then
            MessageBox.Show("Error ICubeSDK_SaveAvi", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If

    End Sub

    Private Sub ButtonSaveCallbackToBMP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSaveCallbackToBMP.Click
        gSaveBmp = True         ' save callback data to a bmp-File
        gSaveCallback = True
    End Sub

End Class
