using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using Matrox.MatroxImagingLibrary;
namespace MDispOverlay
{
class Program
{
private const string IMAGE_FILE = MIL.M_IMAGE_PATH + "Board.mim";
private const string WINDOW_TITLE = "Custom Title";
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilDigitizer = MIL.M_NULL;
MIL_ID MilImage = MIL.M_NULL;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
if (MIL.MsysInquire(MilSystem, MIL.M_DIGITIZER_NUM, MIL.M_NULL) > 0)
{
MIL.MdigAlloc(MilSystem, MIL.M_DEFAULT, "M_DEFAULT", MIL.M_DEFAULT, ref MilDigitizer);
MIL.MbufAllocColor(MilSystem,
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_BAND, MIL.M_NULL),
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_X, MIL.M_NULL),
MIL.MdigInquire(MilDigitizer, MIL.M_SIZE_Y, MIL.M_NULL),
8 + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_DISP + MIL.M_PROC + MIL.M_GRAB,
ref MilImage);
MIL.MbufClear(MilImage, 0);
}
else
{
MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilImage);
}
MIL.MdispControl(MilDisplay, MIL.M_TITLE, WINDOW_TITLE);
MIL.MdispSelect(MilDisplay, MilImage);
OverlayDraw(MilDisplay);
if (MilDigitizer != MIL.M_NULL)
MIL.MdigGrabContinuous(MilDigitizer, MilImage);
Console.Write("\nOVERLAY ANNOTATIONS:\n");
Console.Write("--------------------\n\n");
Console.Write("Displaying an image with overlay annotations.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
if (MilDigitizer != MIL.M_NULL)
{
MIL.MdigHalt(MilDigitizer);
MIL.MdigFree(MilDigitizer);
Console.Write("Displaying the last grabbed image.\n");
Console.Write("Press <Enter> to end.\n\n");
Console.ReadKey();
}
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
static void OverlayDraw(MIL_ID MilDisplay)
{
MIL_ID DefaultGraphicContext = MIL.M_DEFAULT;
MIL_ID MilOverlayImage = MIL.M_NULL;
MIL_INT ImageWidth, ImageHeight;
IntPtr hCustomDC = IntPtr.Zero;
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY, MIL.M_ENABLE);
MIL.MdispInquire(MilDisplay, MIL.M_OVERLAY_ID, ref MilOverlayImage);
MIL.MdispControl(MilDisplay, MIL.M_OVERLAY_CLEAR, MIL.M_DEFAULT);
MIL.MdispControl(MilDisplay, 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.MdispControl(MilDisplay, MIL.M_OVERLAY_SHOW, MIL.M_ENABLE);
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);
}
}
}
}