Mouse Events and Reading Pixel Information with csXImage in VB.NET

The demo application for csXImage includes an option to display information about individual pixels. This makes use of the 'OnMouseMove' event of the control to trigger VB.NET code when the mouse cursor is positioned over an image. The information about the colour of the pixel under the mouse pointer is then read and displayed.

When the pixel information option is activated in the VB demo (using 'Tools/Pixel Info' from the menu), a cross-hair cursor is displayed when the mouse pointer is over the image. Pixel information is then read and displayed, as in the screenshot below.

Reading pixel data from an image using VB.NET

In using this event to provide pixel data we have two problems to consider: (1) The event is fired whenever the cursor is over the control, even if it is not over the image. In these cases we do not want information displaying or the cursor changing. (2) The image may be scrolled, which means the X and Y co-ordinates returned by the OnMouseMove event will not correspond to the co-ordinates of the pixel in the image.

These problems can be overcome by using a group of properties that provide read-only information about the status and positioning of the scroll bars. By checking the scroll bar positions and modifying the X and Y co-ordinates as necessary the correct result can be obtained.

The VB.NET code used in this example is listed below:

Private Sub AxImageBox1_OnMouseMove(ByVal sender As Object, ByVal e
    As AxcsXImageTrial.IImageBoxEvents_OnMouseMoveEvent)
    Handles AxImageBox1.OnMouseMove
  Dim OverImage As Boolean
  Dim PixelCol As System.Drawing.Color

  If mnuToolsPixelInfo.Checked Then

    OverImage = True

    With AxImageBox1

      If Not .HasScrollBarHoriz Then
        If e.x > .ImageWidth - 1 Then
          OverImage = False
        End If
      Else
        If e.x > .ScrollBarHorizWidth - 1 Then
          OverImage = False
        Else
          e.x = e.x + .ScrollBarHorizPos
        End If
      End If

      If Not .HasScrollBarVert Then
        If e.y > .ImageHeight - 1 Then
          OverImage = False
        End If
      Else
        If e.y > .ScrollBarVertHeight - 1 Then
          OverImage = False
        Else
          e.y = e.y + .ScrollBarVertPos
        End If
      End If
    End With

  .
  .
  .

  End If

End Sub

In the code above we use a Boolean 'OverImage' to determine whether or not the cursor is over the image. This variable is initially set to true, then a number of checks are made to see if it should be changed to False, or if the co-ordinates should be modified. First we check in the horizontal direction. If there is no horizontal scroll bar, then the variable is only set to false if the cursor is further to the right than the width of the image. If there is a horizontal scroll bar, then we check that the cursor is inside the width of the scroll bar, and if necessary, adjust for the scroll bar position. The same checks are then repeated in the vertical direction.

Finally, we retrieve and display the pixel information, if the cursor is confirmed to be over the image.

If OverImage Then
  Me.Cursor = Cursors.Cross
  lblPixelX.Text = "X: " & e.x
  lblPixelY.Text = "Y: " & e.y
  PixelCol = AxImageBox1.get_PixelColor(e.x, e.y)
  lblPixelRed.Text = "Red: " & Str(PixelCol.R)
  lblPixelGreen.Text = "Green: " & Str(PixelCol.G)
  lblPixelBlue.Text = "Blue: " & Str(PixelCol.B)
  lblPixelCol.BackColor = PixelCol
Else
  Me.Cursor = Cursors.Default
  lblPixelX.Text = "X: n/a"
  lblPixelY.Text = "Y: n/a"
  lblPixelRed.Text = "Red: n/a"
  lblPixelGreen.Text = "Green: n/a"
  lblPixelBlue.Text = "Blue: n/a"
  lblPixelCol.BackColor = Me.BackColor
End If

Using this code, the cursor can be moved over the image, with information about the colour of the pixel under the mouse cursor displayed alongside, as in the screenshot above.

<<Previous page  Next page>>

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.