Public Class Form1

    Private eng As CaoEngine
    Private ctrl As CaoController
    Private file As CaoFile

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' オブジェクトの生成
        eng = New CaoEngine
        ctrl = eng.Workspaces.Item(0).AddController("sample", "CaoProv.OpenCV")
        file = ctrl.AddFile("image", "ID=1")
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ' オブジェクトの解放
        System.Runtime.InteropServices.Marshal.ReleaseComObject(eng)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(ctrl)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(file)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim lArray() As Double
        Dim i As Integer
        Dim lMax As Double
        Dim temp As Long
        Dim Graph As Graphics

        ' ヒストグラムの算出
        lArray = file.Execute("CalcHistEx", 256)

        ' ヒストグラム描画
        For i = 0 To 255
            If lMax < lArray(i) Then
                lMax = lArray(i)
            End If
        Next
        Graph = PictureBox1.CreateGraphics
        Graph.Clear(PictureBox1.BackColor)
        For i = 0 To 255
            temp = (lArray(i)) * PictureBox1.Height / lMax
            Graph.DrawLine(New Pen(Color.Black, 2), _
                           i, PictureBox1.Height, _
                           i, PictureBox1.Height - temp)
        Next
    End Sub
End Class
