Using Mouse Events and Reading Pixel Information with csXImage in VB

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 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

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 code used in this example is listed below:

Private Sub ImageBox1_OnMouseMove(ByVal ShiftState As Integer, _
  ByVal X As Long, ByVal Y As Long)

Dim OverImage As Boolean
Dim PixelCol As OLE_COLOR

If mnuToolsPixelInfo.Checked Then
  OverImage = True
  With ImageBox1
    If Not .HasScrollBarHoriz Then
      If X > .ImageWidth - 1 Then
        OverImage = False
      End If
    Else
      If X > .ScrollBarHorizWidth - 1 Then
        OverImage = False
      Else
        X = X + .ScrollBarHorizPos
      End If
    End If
    If Not .HasScrollBarVert Then
      If X > .ImageHeight - 1 Then
        OverImage = False
      End If
    Else
      If X > .ScrollBarVertHeight - 1 Then
        OverImage = False
      Else
        Y = Y + .ScrollBarVertPos
      End If
    End If
  End With

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
    Screen.MousePointer = vbCrosshair
    lblPixelX.Caption = "X: " & X
    lblPixelY.Caption = "Y: " & Y
    PixelCol = ImageBox1.PixelColor(X, Y)
    lblPixelRed.Caption = "Red: " & Str(PixelCol Mod 256)
    lblPixelGreen.Caption = "Green: " & Str((PixelCol \ 256) Mod 256)
    lblPixelBlue.Caption = "Blue: " & Str((PixelCol \ 65536) Mod 256)
    shpPixelCol.FillColor = PixelCol
  Else
    Screen.MousePointer = vbDefault
    lblPixelX.Caption = "X: n/a"
    lblPixelY.Caption = "Y: n/a"
    lblPixelRed.Caption = "Red: n/a"
    lblPixelGreen.Caption = "Green: n/a"
    lblPixelBlue.Caption = "Blue: n/a"
    shpPixelCol.FillColor = shpPixelCol.Parent.BackColor
  End If

End If

End Sub

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.