Live Display: 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.0.3
Released on:April 7, 2005
Author:IC Imaging Control Support Department
Requirements:IC Imaging Control >2.1
Camera, converter or grabber with WDM Stream Class drivers.

The main window of the resulting application looks as follows:

The dialog window of the sample application.

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;

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, TIS.Imaging.ICImagingControl.MouseEventArgs e)
{
    if( (e.ModifierKeys & Keys.Shift) == 0 )
    {
        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, TIS.Imaging.ICImagingControl.MouseEventArgs e)
{
    if( bDragging )
    {
        if( (e.ModifierKeys & Keys.Shift) == 0 )
        {
            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, TIS.Imaging.ICImagingControl.MouseEventArgs e)
{
    bDragging = false;
}

Related Source Code Samples

Disclaimer
The source code that appears in the IC Imaging Control Source Code Library is indented for educational purposes only. The Imaging Source Europe GmbH, the manufacturer of IC Imaging Control, does not assume any kind of warranty expressed or implied, resulting from the use of the content of this page.

This site is part of The Imaging Source Network. Other sites include Company Portal, Image Processing, Astronomy Cameras, Astronomy Cameras Blog, Blog caméras d'astronomie, Astronomy Cameras Competition, TX Text Control, LiveDocx, phpLiveDocx and Forums.