Movable Crosshair on an Overlay Short source code snippet that illustrates how to overlay an image data stream with movable crosshair.
Language: | .NET C#/Visual Basic
Version: | 3.3
Author: | IC Imaging Control Support Department
Requirements: |
Software: | IC Imaging Control 3.3, Visual Studio™ 2010
Hardware: | Camera, converter or grabber with WDM Stream Class drivers.
Download C# sample | Movable Crosshairs - C#

The main window of the resulting application looks as follows:

Additionally, the current position of the crosshair is displayed.

The program starts by activating a built-in dialog to select a device (.ShowDeviceSettingsDialog). At the end of the function ( Form_Load() ), the device's live image data stream is displayed, using .LiveStart. In preparation, there are two additional steps: Firstly, the overlay mode has to be activated (.OverlayBitmap.Enable) and the status variable dragging has to be set to False. We are going to use this status variable to indicate that the mouse button is kept pressed down:

    C#

    bool bDragging = false;
    bool ShiftPressed = false;

    private void Form1_Load(object sender, System.EventArgs e)
    {
        icImagingControl1.ShowDeviceSettingsDialog();

        if( !icImagingControl1.DeviceValid )
        {
            Close();
            return;
        }

        icImagingControl1.OverlayBitmap.Enable = true;
        icImagingControl1.OverlayBitmap.ColorMode = TIS.Imaging.OverlayColorModes.Color;
        icImagingControl1.LiveStart();
    }

A crosshair consist of two red lines with an attached blue text, indicating the cross' position (see image on the right). .OverlayBitmap.DrawLine draws the lines, while .OverlayBitmap.DrawText generates the text:

    C#

    private void DrawCrosshairs(int x, int y)
    {
        icImagingControl1.OverlayBitmap.DrawLine(Color.Red, x, y - 10, x, y + 10);
        icImagingControl1.OverlayBitmap.DrawLine(Color.Red, x - 10, y, x + 10, y);
        icImagingControl1.OverlayBitmap.DrawText(Color.Blue, x + 3, y + 2, x.ToString() + "," + y.ToString());
    }

There are two factors determining the position of the crosshair: the mouse's movement and it's button state. The following three event handlers perform the necessary processing. When the user clicks into the image, the program removes the "old" crosshair, filling the overlay bitmap with the transparent color .DropOutColor. DisplayCrosshairs XPos, YPos draws a new crosshair at the mouse's position:

    C#

    private void icImagingControl1_MouseDown(object sender, MouseEventArgs e)
    {
        if (!ShiftPressed)
        {
            icImagingControl1.OverlayBitmap.Fill( icImagingControl1.OverlayBitmap.DropOutColor );
        }

        DrawCrosshairs( e.X, e.Y );

        bDragging = true;
    }

When the user moves the mouse, while pressing the mouse button, the program removes the "old" crosshair, filling the overlay bitmap with the transparent color .DropOutColor. DisplayCrosshairs XPos, YPos draws a new crosshair at the mouse's position:

    C#

    private void icImagingControl1_MouseMove(object sender, MouseEventArgs e)
    {
        if( bDragging )
        {
            if (!ShiftPressed)
            {
                icImagingControl1.OverlayBitmap.Fill( icImagingControl1.OverlayBitmap.DropOutColor );
            }

            DrawCrosshairs( e.X, e.Y );
        }
    }

Releasing the mouse button leads to the status variable dragging being set to False:

    C#

    private void icImagingControl1_MouseUp(object sender, MouseEventArgs e)
    {
        bDragging = false;
    }

The bool member variable ShiftPressed is set in the Imaging Control key event handlers:

    C#

    private void icImagingControl1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Shift)
        {
            ShiftPressed = true;
        }
    }

    private void icImagingControl1_KeyUp(object sender, KeyEventArgs e)
    {
        ShiftPressed = false;
    }