using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms.Integration;
using Matrox.MatroxImagingLibrary;
namespace MILApplicationLibrary
{
public class MILApplication : INotifyPropertyChanged
{
#region Constants
private const int DEFAULT_IMAGE_SIZE_X = 640;
private const int DEFAULT_IMAGE_SIZE_Y = 480;
private const int DEFAULT_IMAGE_SIZE_BAND = 1;
#endregion
#region Constructor
public MILApplication()
{
_appId = MIL.M_NULL;
_sysId = MIL.M_NULL;
_digId = MIL.M_NULL;
_dispId = MIL.M_NULL;
_bufId = MIL.M_NULL;
}
#endregion
#region Public methods
public void Allocate()
{
MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, ref _appId);
MIL.MsysAlloc(MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, MIL.M_DEFAULT, ref _sysId);
MIL.MdispAlloc(_sysId, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_WINDOWED, ref _dispId);
MIL_INT bufferSizeX = DEFAULT_IMAGE_SIZE_X;
MIL_INT bufferSizeY = DEFAULT_IMAGE_SIZE_Y;
MIL_INT bufferSizeBand = DEFAULT_IMAGE_SIZE_BAND;
long imageAttributes = MIL.M_IMAGE | MIL.M_DISP | MIL.M_PROC;
MIL_INT numberOfDigitizers = MIL.MsysInquire(_sysId, MIL.M_DIGITIZER_NUM, MIL.M_NULL);
if (numberOfDigitizers > 0)
{
MIL.MdigAlloc(_sysId, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, ref _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 |= MIL.M_GRAB;
}
RaisePropertyChangedEvent("CanStartGrab");
RaisePropertyChangedEvent("CanStopGrab");
MIL.MbufAllocColor(_sysId, bufferSizeBand, bufferSizeX, bufferSizeY, 8 + MIL.M_UNSIGNED, imageAttributes, ref _bufId);
RaisePropertyChangedEvent("BufferSizeX");
RaisePropertyChangedEvent("BufferSizeY");
FillImageBuffer(bufferSizeX, bufferSizeY);
}
public void SetDisplayHost(WindowsFormsHost displayHost)
{
if (displayHost == null)
throw new ArgumentNullException("displayHost", "Must be a valid object.");
MIL.MdispSelectWPF(_dispId, _bufId, displayHost);
InitializeOverlay();
}
public void StartGrab()
{
MIL.MdigGrabContinuous(_digId, _bufId);
_isGrabbing = true;
RaisePropertyChangedEvent("CanStartGrab");
RaisePropertyChangedEvent("CanStopGrab");
}
public void StopGrab()
{
MIL.MdigHalt(_digId);
_isGrabbing = false;
RaisePropertyChangedEvent("CanStartGrab");
RaisePropertyChangedEvent("CanStopGrab");
}
public void Free()
{
if (CanStopGrab)
{
StopGrab();
}
if (_dispId != MIL.M_NULL)
{
MIL_ID selectedBufferId = (MIL_ID)MIL.MdispInquire(_dispId, MIL.M_SELECTED, MIL.M_NULL);
if (selectedBufferId != MIL.M_NULL)
{
MIL.MdispSelectWPF(_dispId, MIL.M_NULL, null);
}
MIL.MdispFree(_dispId);
_dispId = MIL.M_NULL;
}
if (_bufId != MIL.M_NULL)
{
MIL.MbufFree(_bufId);
_bufId = MIL.M_NULL;
}
if (_digId != MIL.M_NULL)
{
MIL.MdigFree(_digId);
_digId = MIL.M_NULL;
}
if (_sysId != MIL.M_NULL)
{
MIL.MsysFree(_sysId);
_sysId = MIL.M_NULL;
}
if (_appId != MIL.M_NULL)
{
MIL.MappFree(_appId);
_appId = MIL.M_NULL;
}
GC.SuppressFinalize(this);
}
#endregion
#region Properties used in data bindings
public bool CanStartGrab
{
get { return ((_digId != MIL.M_NULL) && (!_isGrabbing)); }
}
public bool CanStopGrab
{
get { return ((_digId != MIL.M_NULL) && (_isGrabbing)); }
}
public int BufferSizeX
{
get { return GetBufferSize(MIL.M_SIZE_X); }
}
public int BufferSizeY
{
get { return GetBufferSize(MIL.M_SIZE_Y); }
}
public bool OverlayVisible
{
get
{
bool overlayVisible = false;
if (_dispId != MIL.M_NULL)
{
MIL_INT overlayShow = 0;
MIL.MdispInquire(_dispId, MIL.M_OVERLAY_SHOW, ref overlayShow);
if (overlayShow == MIL.M_ENABLE)
{
overlayVisible = true;
}
}
return overlayVisible;
}
set
{
bool overlayVisible = value;
if (_dispId != MIL.M_NULL)
{
MIL_INT controlValue = MIL.M_DISABLE;
if (overlayVisible)
{
controlValue = MIL.M_ENABLE;
if (!_overlayInitialized)
{
InitializeOverlay();
}
}
MIL.MdispControl(_dispId, MIL.M_OVERLAY_SHOW, controlValue);
}
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Helper functions
private int GetBufferSize(long inquireType)
{
int bufferSize = 0;
if (_bufId != MIL.M_NULL)
{
bufferSize = (int)MIL.MbufInquire(_bufId, inquireType, MIL.M_NULL);
}
return bufferSize;
}
private void FillImageBuffer(MIL_INT bufferSizeX, MIL_INT bufferSizeY)
{
MIL.MbufClear(_bufId, MIL.M_RGB888(0, 0, 0));
MIL_INT defaultGraFont = 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);
}
private void InitializeOverlay()
{
MIL_ID defaultGraphicContext = MIL.M_DEFAULT;
MIL_ID milOverlayImage = MIL.M_NULL;
MIL_INT imageWidth, imageHeight;
IntPtr hCustomDC = IntPtr.Zero;
MIL.MdispControl(_dispId, MIL.M_OVERLAY, MIL.M_ENABLE);
MIL.MdispInquire(_dispId, MIL.M_OVERLAY_ID, ref 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 = (IntPtr)MIL.MbufInquire(milOverlayImage, MIL.M_DC_HANDLE, MIL.M_NULL);
MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE);
if (!hCustomDC.Equals(IntPtr.Zero))
{
using (Graphics DrawingGraphics = Graphics.FromHdc(hCustomDC))
{
using (Pen DrawingPen = new Pen(Color.Blue))
{
DrawingGraphics.DrawLine(DrawingPen, 0, (int)(imageHeight / 2), imageWidth, (int)(imageHeight / 2));
DrawingGraphics.DrawLine(DrawingPen, (int)(imageWidth / 2), 0, (int)(imageWidth / 2), imageHeight);
using (SolidBrush LeftBrush = new SolidBrush(Color.Red))
{
using (SolidBrush RightBrush = new SolidBrush(Color.Yellow))
{
using (Font OverlayFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold))
{
SizeF GDITextSize = 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));
}
}
}
}
}
MIL.MbufControl(milOverlayImage, MIL.M_DC_FREE, MIL.M_DEFAULT);
MIL.MbufControl(milOverlayImage, MIL.M_MODIFIED, MIL.M_DEFAULT);
}
_overlayInitialized = true;
}
private void RaisePropertyChangedEvent(string propertyName)
{
VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
Debug.Fail("Invalid property name: " + propertyName);
}
}
#endregion
#region Private members
private MIL_ID _appId;
private MIL_ID _sysId;
private MIL_ID _digId;
private MIL_ID _dispId;
private MIL_ID _bufId;
private bool _isGrabbing;
private bool _overlayInitialized;
#endregion
}
}