Click here to Skip to main content
14,391,319 members

Need to get pixel info and scroll image using VB .NET

larry118 asked:

Open original thread

I found this neat little example on scrolling a regular picturebox in vb .net a while ago. I apologize because I can't give credit where credit is due, but it works great for zooming except for one little problem, actually two.

I need to show some info while mousing over the image such as rgb values. The other is to be able to zoom in and change a pixel value like a paint program.

In the example I have a mouseover event in the picturebox that works pretty well to display the rgb value before the image is zooomed in or out. Once the picturebox changes size the rgb is no longer accurate and does not work at all when outside the original picture boundaries.

I can resize the image but the mousewheel zoom works so well I was wondering if anyone could help me make the mouse over event work when the image is zoomed or panned. I have provided a simple project as an example.

Again I apologize to whoever posted this solution, it works great for its original purpose.


What I have tried:

'Getting screen coordinates and mapping them to the image. Here's the code:

Imports System.ComponentModel

Public Class Form1
    Private ZOOMFACTOR As Double = 1.25

    ' = 25% smaller or larger
    Private MINMAX As Integer = 5
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' PicBox
        Me.PicBox.Location = New System.Drawing.Point(120, 120)
        Me.PicBox.Name = "PicBox"
        Me.PicBox.Size = New System.Drawing.Size(PicBox.Image.Width / 3, PicBox.Image.Height / 3)
        Me.PicBox.TabIndex = 3
        Me.PicBox.TabStop = False
        ' OuterPanel
        Me.OuterPanel.AutoScroll = True
        Me.OuterPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        OuterPanel.Cursor = System.Windows.Forms.Cursors.NoMove2D

        AddHandler OuterPanel.MouseEnter, AddressOf Me.PicBox_MouseEnter
        AddHandler PicBox.MouseEnter, AddressOf Me.PicBox_MouseEnter
        AddHandler OuterPanel.MouseWheel, AddressOf Me.PicBox_MouseWheel
        '   Me.OuterPanel.Controls.Add(Me.PicBox)
        '   Me.OuterPanel.Dock = System.Windows.Forms.DockStyle.Fill
        '   Me.OuterPanel.Location = New System.Drawing.Point(0, 0)
        '  Me.OuterPanel.Name = "OuterPanel"
        Me.OuterPanel.Size = New System.Drawing.Size(210, 190)
        Me.OuterPanel.TabIndex = 4
        ' PictureBox
        '  Me.Controls.Add(Me.OuterPanel)
        Me.Name = "PictureBox"
        Me.Size = New System.Drawing.Size(1000, 1000)
    End Sub

    Private Sub PicBox_MouseWheel(sender As Object, e As MouseEventArgs) Handles PicBox.MouseWheel
        If (e.Delta < 0) Then
        End If
    End Sub

    Private Sub PicBox_MouseEnter(sender As Object, e As EventArgs) Handles PicBox.MouseEnter
        If (Me.PicBox.Focused = False) Then
        End If
    End Sub
    Private Sub ZoomIn()
        If ((Me.PicBox.Width _
                    < (Me.MINMAX * Me.OuterPanel.Width)) _
                    AndAlso (Me.PicBox.Height _
                    < (Me.MINMAX * Me.OuterPanel.Height))) Then
            Me.PicBox.Width = Convert.ToInt32((Me.PicBox.Width * Me.ZOOMFACTOR))
            Me.PicBox.Height = Convert.ToInt32((Me.PicBox.Height * Me.ZOOMFACTOR))
            Me.PicBox.SizeMode = PictureBoxSizeMode.StretchImage
        End If

    End Sub

    ''' <summary>
    ''' Make the PictureBox dimensions smaller to effect the Zoom.
    ''' </summary>
    ''' <remarks>Minimum 5 times smaller</remarks>
    Private Sub ZoomOut()
        If ((Me.PicBox.Width _
                    > (Me.OuterPanel.Width / Me.MINMAX)) _
                    AndAlso (Me.PicBox.Height _
                    > (Me.OuterPanel.Height / Me.MINMAX))) Then
            Me.PicBox.SizeMode = PictureBoxSizeMode.StretchImage
            Me.PicBox.Width = Convert.ToInt32((Me.PicBox.Width / Me.ZOOMFACTOR))
            Me.PicBox.Height = Convert.ToInt32((Me.PicBox.Height / Me.ZOOMFACTOR))
        End If

    End Sub

    Private Sub PicBox_MouseMove(sender As Object, e As MouseEventArgs) Handles PicBox.MouseMove
        If PicBox.Image Is Nothing Then
            Exit Sub
        End If
        Dim bmpRGB As Bitmap = PicBox.Image

            Cursor.Current = Cursors.Hand

            'If e.X > (bmpRGB.Width * CInt(lblZoom.Text) - 1) Or e.Y > (bmpRGB.Height * CInt(lblZoom.Text) - 1) Then Exit Sub
            Dim color = bmpRGB.GetPixel(e.X, e.Y)
            lblColor.Text = "R=: " + color.R.ToString + " G=: " + color.G.ToString + " B=: " + color.B.ToString
            Cursor.Current = Cursors.Default
            Cursor.Current = Cursors.Default
            '    bmpRGB.Dispose()
            Cursor.Current = Cursors.Default
        End Try
    End Sub
End Class
Tags: VB.NET, processing, image


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100