'
'
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Windows.Forms.Integration
Imports Matrox.MatroxImagingLibrary
Public Class MILApplication
Implements INotifyPropertyChanged
#Region "Constants"
Private Const DEFAULT_IMAGE_SIZE_X As Integer = 640
Private Const DEFAULT_IMAGE_SIZE_Y As Integer = 480
Private Const DEFAULT_IMAGE_SIZE_BAND As Integer = 1
#End Region
#Region "Constructor"
Public Sub New()
_appId = MIL.M_NULL
_sysId = MIL.M_NULL
_digId = MIL.M_NULL
_dispId = MIL.M_NULL
_bufId = MIL.M_NULL
End Sub
#End Region
#Region "Public methods"
Public Sub Allocate()
MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, _appId)
MIL.MsysAlloc(MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, MIL.M_DEFAULT, _sysId)
MIL.MdispAlloc(_sysId, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_WINDOWED, _dispId)
Dim bufferSizeX As MIL_INT = DEFAULT_IMAGE_SIZE_X
Dim bufferSizeY As MIL_INT = DEFAULT_IMAGE_SIZE_Y
Dim bufferSizeBand As MIL_INT = DEFAULT_IMAGE_SIZE_BAND
Dim imageAttributes As Long = MIL.M_IMAGE Or MIL.M_DISP Or MIL.M_PROC
Dim numberOfDigitizers As MIL_INT = MIL.MsysInquire(_sysId, MIL.M_DIGITIZER_NUM, MIL.M_NULL)
If numberOfDigitizers > 0 Then
MIL.MdigAlloc(_sysId, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, _digId)
bufferSizeBand = MIL.MdigInquire(_digId, MIL.M_SIZE_BAND, MIL.M_NULL)
bufferSizeX = MIL.MdigInquire(_digId, MIL.M_SIZE_X, MIL.M_NULL)
bufferSizeY = MIL.MdigInquire(_digId, MIL.M_SIZE_Y, MIL.M_NULL)
imageAttributes = imageAttributes Or MIL.M_GRAB
End If
RaisePropertyChangedEvent("CanStartGrab")
RaisePropertyChangedEvent("CanStopGrab")
MIL.MbufAllocColor(_sysId, bufferSizeBand, bufferSizeX, bufferSizeY, 8 + MIL.M_UNSIGNED, imageAttributes, _bufId)
RaisePropertyChangedEvent("BufferSizeX")
RaisePropertyChangedEvent("BufferSizeY")
FillImageBuffer(bufferSizeX, bufferSizeY)
End Sub
Public Sub SetDisplayHost(ByVal displayHost As WindowsFormsHost)
If displayHost Is Nothing Then
Throw New ArgumentNullException("displayHost", "Must be a valid object.")
End If
MIL.MdispSelectWPF(_dispId, _bufId, displayHost)
InitializeOverlay()
End Sub
Public Sub StartGrab()
MIL.MdigGrabContinuous(_digId, _bufId)
_isGrabbing = True
RaisePropertyChangedEvent("CanStartGrab")
RaisePropertyChangedEvent("CanStopGrab")
End Sub
Public Sub StopGrab()
MIL.MdigHalt(_digId)
_isGrabbing = False
RaisePropertyChangedEvent("CanStartGrab")
RaisePropertyChangedEvent("CanStopGrab")
End Sub
Public Sub Free()
If CanStopGrab Then
StopGrab()
End If
If _dispId <> MIL.M_NULL Then
Dim selectedBufferId As MIL_ID = CType(MIL.MdispInquire(_dispId, MIL.M_SELECTED, MIL.M_NULL), MIL_ID)
If selectedBufferId <> MIL.M_NULL Then
MIL.MdispSelectWPF(_dispId, MIL.M_NULL, Nothing)
End If
MIL.MdispFree(_dispId)
_dispId = MIL.M_NULL
End If
If _bufId <> MIL.M_NULL Then
MIL.MbufFree(_bufId)
_bufId = MIL.M_NULL
End If
If _digId <> MIL.M_NULL Then
MIL.MdigFree(_digId)
_digId = MIL.M_NULL
End If
If _sysId <> MIL.M_NULL Then
MIL.MsysFree(_sysId)
_sysId = MIL.M_NULL
End If
If _appId <> MIL.M_NULL Then
MIL.MappFree(_appId)
_appId = MIL.M_NULL
End If
GC.SuppressFinalize(Me)
End Sub
#End Region
#Region "Properties used in data bindings"
Public ReadOnly Property CanStartGrab() As Boolean
Get
Return ((_digId <> MIL.M_NULL) And (Not _isGrabbing))
End Get
End Property
Public ReadOnly Property CanStopGrab() As Boolean
Get
Return ((_digId <> MIL.M_NULL) And _isGrabbing)
End Get
End Property
Public ReadOnly Property BufferSizeX() As Integer
Get
Return GetBufferSize(MIL.M_SIZE_X)
End Get
End Property
Public ReadOnly Property BufferSizeY() As Integer
Get
Return GetBufferSize(MIL.M_SIZE_Y)
End Get
End Property
Public Property OverlayVisible() As Boolean
Get
Dim overlayVisibleValue As Boolean = False
If _dispId <> MIL.M_NULL Then
Dim overlayShow As MIL_INT = 0
MIL.MdispInquire(_dispId, MIL.M_OVERLAY_SHOW, overlayShow)
If overlayShow = MIL.M_ENABLE Then
overlayVisibleValue = True
End If
End If
Return overlayVisibleValue
End Get
Set(ByVal value As Boolean)
Dim overlayVisibleValue As Boolean = value
If _dispId <> MIL.M_NULL Then
Dim controlValue As MIL_INT = MIL.M_DISABLE
If overlayVisibleValue Then
If (Not _overlayInitialized) Then
InitializeOverlay()
End If
controlValue = MIL.M_ENABLE
End If
MIL.MdispControl(_dispId, MIL.M_OVERLAY_SHOW, controlValue)
End If
End Set
End Property
#End Region
#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
#End Region
#Region "Helper functions"
Private Function GetBufferSize(ByVal inquireType As Long) As Integer
Dim bufferSize As Integer = 0
If _bufId <> MIL.M_NULL Then
bufferSize = CInt(Fix(MIL.MbufInquire(_bufId, inquireType, MIL.M_NULL)))
End If
Return bufferSize
End Function
Private Sub FillImageBuffer(ByVal bufferSizeX As MIL_INT, ByVal bufferSizeY As MIL_INT)
MIL.MbufClear(_bufId, MIL.M_RGB888(0, 0, 0))
Dim defaultGraFont As MIL_INT = MIL.MgraInquire(MIL.M_DEFAULT, MIL.M_FONT, MIL.M_NULL)
MIL.MgraFont(MIL.M_DEFAULT, MIL.M_FONT_DEFAULT_LARGE)
MIL.MgraText(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2), bufferSizeY / 2, " Welcome to MIL !!! ")
MIL.MgraRect(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2) - 60, (bufferSizeY / 2) - 80, ((bufferSizeX / 8) * 2) + 370, (bufferSizeY / 2) + 100)
MIL.MgraRect(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2) - 40, (bufferSizeY / 2) - 60, ((bufferSizeX / 8) * 2) + 350, (bufferSizeY / 2) + 80)
MIL.MgraRect(MIL.M_DEFAULT, _bufId, ((bufferSizeX / 8) * 2) - 20, (bufferSizeY / 2) - 40, ((bufferSizeX / 8) * 2) + 330, (bufferSizeY / 2) + 60)
MIL.MgraFont(MIL.M_DEFAULT, defaultGraFont)
End Sub
Private Sub InitializeOverlay()
Dim DefaultGraphicContext As MIL_ID = MIL.M_DEFAULT
Dim MilOverlayImage As MIL_ID = MIL.M_NULL
Dim ImageWidth, ImageHeight As MIL_INT
Dim hCustomDC As IntPtr = IntPtr.Zero
MIL.MdispControl(_dispId, MIL.M_OVERLAY, MIL.M_ENABLE)
MIL.MdispInquire(_dispId, MIL.M_OVERLAY_ID, MilOverlayImage)
MIL.MdispControl(_dispId, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT)
MIL.MdispControl(_dispId, MIL.M_OVERLAY_SHOW, MIL.M_DISABLE)
ImageWidth = MIL.MbufInquire(MilOverlayImage, MIL.M_SIZE_X, MIL.M_NULL)
ImageHeight = MIL.MbufInquire(MilOverlayImage, MIL.M_SIZE_Y, MIL.M_NULL)
MIL.MgraControl(DefaultGraphicContext, MIL.M_BACKGROUND_MODE, MIL.M_TRANSPARENT)
MIL.MgraColor(DefaultGraphicContext, MIL.M_COLOR_WHITE)
MIL.MgraText(DefaultGraphicContext, MilOverlayImage, ImageWidth / 9, ImageHeight / 5, " -------------------- ")
MIL.MgraText(DefaultGraphicContext, MilOverlayImage, ImageWidth / 9, ImageHeight / 5 + 25, " - MIL Overlay Text - ")
MIL.MgraText(DefaultGraphicContext, MilOverlayImage, ImageWidth / 9, ImageHeight / 5 + 50, " -------------------- ")
MIL.MgraColor(DefaultGraphicContext, MIL.M_COLOR_GREEN)
MIL.MgraText(DefaultGraphicContext, MilOverlayImage, ImageWidth * 11 / 18, ImageHeight / 5, " ---------------------")
MIL.MgraText(DefaultGraphicContext, MilOverlayImage, ImageWidth * 11 / 18, ImageHeight / 5 + 25, " - MIL Overlay Text - ")
MIL.MgraText(DefaultGraphicContext, MilOverlayImage, ImageWidth * 11 / 18, ImageHeight / 5 + 50, " ---------------------")
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE)
MIL.MbufControl(MilOverlayImage, MIL.M_DC_ALLOC, MIL.M_DEFAULT)
hCustomDC = CType(MIL.MbufInquire(MilOverlayImage, MIL.M_DC_HANDLE, MIL.M_NULL), IntPtr)
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE)
If (Not hCustomDC.Equals(IntPtr.Zero)) Then
Using DrawingGraphics As Graphics = Graphics.FromHdc(hCustomDC)
Using DrawingPen As New Pen(Color.Blue)
DrawingGraphics.DrawLine(DrawingPen, 0, CInt(Fix(ImageHeight / 2)), ImageWidth, CInt(Fix(ImageHeight / 2)))
DrawingGraphics.DrawLine(DrawingPen, CInt(Fix(ImageWidth / 2)), 0, CInt(Fix(ImageWidth / 2)), ImageHeight)
Using LeftBrush As New SolidBrush(Color.Red)
Using RightBrush As New SolidBrush(Color.Yellow)
Using OverlayFont As New Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold)
Dim GDITextSize As SizeF = DrawingGraphics.MeasureString("GDI Overlay Text", OverlayFont)
DrawingGraphics.DrawString("GDI Overlay Text", OverlayFont, LeftBrush, System.Convert.ToInt32(ImageWidth / 4 - GDITextSize.Width / 2), System.Convert.ToInt32(ImageHeight * 3 / 4 - GDITextSize.Height / 2))
DrawingGraphics.DrawString("GDI Overlay Text", OverlayFont, RightBrush, System.Convert.ToInt32(ImageWidth * 3 / 4 - GDITextSize.Width / 2), System.Convert.ToInt32(ImageHeight * 3 / 4 - GDITextSize.Height / 2))
End Using
End Using
End Using
End Using
End Using
MIL.MbufControl(MilOverlayImage, MIL.M_DC_FREE, MIL.M_DEFAULT)
MIL.MbufControl(MilOverlayImage, MIL.M_MODIFIED, MIL.M_DEFAULT)
End If
End Sub
Private Sub RaisePropertyChangedEvent(ByVal propertyName As String)
VerifyPropertyName(propertyName)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
<Conditional("DEBUG"), DebuggerStepThrough()> _
Public Sub VerifyPropertyName(ByVal propertyName As String)
If TypeDescriptor.GetProperties(Me)(propertyName) Is Nothing Then
Debug.Fail("Invalid property name: " & propertyName)
End If
End Sub
#End Region
#Region "Private members"
Private _appId As MIL_ID
Private _sysId As MIL_ID
Private _digId As MIL_ID
Private _dispId As MIL_ID
Private _bufId As MIL_ID
Private _isGrabbing As Boolean
Private _overlayInitialized As Boolean
#End Region
End Class