Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents btnSave As System.Windows.Forms.Button
    Friend WithEvents optionGroupBox As System.Windows.Forms.GroupBox
    Friend WithEvents txtOption As System.Windows.Forms.TextBox
    Friend WithEvents bntAddController As System.Windows.Forms.Button
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.btnSave = New System.Windows.Forms.Button()
        Me.optionGroupBox = New System.Windows.Forms.GroupBox()
        Me.txtOption = New System.Windows.Forms.TextBox()
        Me.bntAddController = New System.Windows.Forms.Button()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.optionGroupBox.SuspendLayout()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(829, 399)
        Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Button1.Location = New System.Drawing.Point(731, 402)
        Me.Button1.Margin = New System.Windows.Forms.Padding(0)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "GetImage"
        '
        'btnSave
        '
        Me.btnSave.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnSave.Location = New System.Drawing.Point(731, 429)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.Size = New System.Drawing.Size(96, 23)
        Me.btnSave.TabIndex = 2
        Me.btnSave.Text = "Save..."
        Me.btnSave.UseVisualStyleBackColor = True
        '
        'optionGroupBox
        '
        Me.optionGroupBox.Controls.Add(Me.txtOption)
        Me.optionGroupBox.Location = New System.Drawing.Point(0, 402)
        Me.optionGroupBox.Name = "optionGroupBox"
        Me.optionGroupBox.Size = New System.Drawing.Size(528, 50)
        Me.optionGroupBox.TabIndex = 3
        Me.optionGroupBox.TabStop = False
        Me.optionGroupBox.Text = "Option"
        '
        'txtOption
        '
        Me.txtOption.Location = New System.Drawing.Point(4, 20)
        Me.txtOption.Name = "txtOption"
        Me.txtOption.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal
        Me.txtOption.Size = New System.Drawing.Size(524, 19)
        Me.txtOption.TabIndex = 0
        '
        'bntAddController
        '
        Me.bntAddController.Location = New System.Drawing.Point(534, 422)
        Me.bntAddController.Name = "bntAddController"
        Me.bntAddController.Size = New System.Drawing.Size(125, 23)
        Me.bntAddController.TabIndex = 4
        Me.bntAddController.Text = "Add controller"
        Me.bntAddController.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.AutoSize = True
        Me.ClientSize = New System.Drawing.Size(829, 453)
        Me.Controls.Add(Me.bntAddController)
        Me.Controls.Add(Me.optionGroupBox)
        Me.Controls.Add(Me.btnSave)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.optionGroupBox.ResumeLayout(False)
        Me.optionGroupBox.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region


    ' CAOエンジン
    Private caoEng As CaoEngine = Nothing
    ' CAOワークスペース
    Private caoWS As CaoWorkspace = Nothing
    ' USBカメラのCAOコントローラ
    Private ctrlCamera As CaoController = Nothing
    ' カメラ画像のCAOファイル
    Private m_filePicture As CaoFile = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'CAOエンジンとCAOワークスペースの作成
        caoEng = New CaoEngine
        caoWS = caoEng.Workspaces.Item(0)
        txtOption.Text = String.Empty
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ctrlCamera Is Nothing Then
            Return
        End If

        Dim bArray() As Byte
        ' 画像データの取得
        bArray = m_filePicture.Value
        ' バイナリデータからMemoryStreamオブジェクトを作成する
        Dim mstreamCur As New MemoryStream(bArray)
        ' ストリームからImageオブジェクトを作成し，ピクチャボックスに表示
        PictureBox1.Image = Image.FromStream(mstreamCur)
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        If ctrlCamera Is Nothing Then
            Return
        End If

        If (Me.PictureBox1.Image Is Nothing) Then
            Return
        End If

        Dim saveFileDialog As New SaveFileDialog()
        Dim picImageFormat As ImageFormat = Me.PictureBox1.Image.RawFormat
        If picImageFormat.Equals(ImageFormat.Bmp) Then
            saveFileDialog.Filter = "BMP(*.bmp)|*.bmp;|すべてのファイル(*.*)|*.*"
        ElseIf picImageFormat.Equals(ImageFormat.Jpeg) Then
            saveFileDialog.Filter = "JPEG(*.jpeg;jpg)|*.jpeg;jpg|すべてのファイル(*.*)|*.*"
        End If

        saveFileDialog.FileName = "新しい画像ファイル"
        saveFileDialog.Title = "画像の保存先を選択してください"
        If (saveFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Me.PictureBox1.Image.Save(saveFileDialog.FileName)
        End If
    End Sub

    Private Sub bntAddController_Click(sender As System.Object, e As System.EventArgs) Handles bntAddController.Click
        If Not (ctrlCamera Is Nothing) Then
            If Not (m_filePicture Is Nothing) Then
                ctrlCamera.Files.Remove(m_filePicture.Index)
            End If
            caoWS.Controllers.Remove(ctrlCamera.Index)

            Marshal.ReleaseComObject(m_filePicture)
            Marshal.ReleaseComObject(ctrlCamera)

            ctrlCamera = Nothing
            m_filePicture = Nothing
        End If
        ctrlCamera = caoWS.AddController("Cam1", "CaoProv.DirectShow", "", Me.txtOption.Text)
        ' CAOファイルの取得
        m_filePicture = ctrlCamera.AddFile("Picture")
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        Marshal.ReleaseComObject(caoEng)
        caoEng = Nothing
    End Sub
End Class
