[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

How to move an image with mouse down in a picture box

I have an image in a picture box, I need the user to be able to move the image around while the mouse button is down, anyone have any ideas how to do this?
0
eneate
Asked:
eneate
  • 3
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The easiest way is to use TWO PictureBoxes, with one PictureBox INSIDE the other.

So in this example, Picture2 is INSIDE Picture1.  The image is loaded into Picture2 with the AutoSize property set to True.  Left click and drag to "pan" the image around:

Option Explicit

Private startX As Single
Private startY As Single

Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        startX = X
        startY = Y
    End If
End Sub

Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        Picture2.Move Picture2.Left + (X - startX), Picture2.Top + (Y - startY)
    End If
End Sub
0
 
eneateAuthor Commented:
Thanks for the reply.

I can move the image around but it only shows the original small section that was loaded with the loadpicture(img). The ima i s larger than the frame I need to be able to move around to see different sections of the image. Is it also possible to constrain the edges so that the whiole box stays inside the first picture box
0
 
eneateAuthor Commented:
i have tried using refresh but unless it is in move it doesn't work but placing ti in move causes the screan to flick annoyingly all the time, there must be a beter alternative ideas? Chow
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"it only shows the original small section that was loaded with the loadpicture(img)."

    As I said before, you need to set the AutoSize() property of the PictureBox with the Image in it to True.

"Is it also possible to constrain the edges so that the whiole box stays inside the first picture box"

    I'll post back in couple minutes...  =)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a solution that addresses both of your questions with respect to panning and zooming an image.

Create a New Project.
Click on Project --> Components and check the "Microsoft Common Dialog Control ..." entry.
Add a CommonDialog control to your Form.
Across the top of the form, add FOUR buttons called btnLoad, btnZoomIn, btnZoomOut, and btnReset.
Add a Label (Label1) to the right of the last button.
Add a PictureBox (Picture1) below the four buttons so that it takes up the rest of the form.
Select Picture1 by clicking on it.
Now INSIDE Picture1, add a second PictureBox (Picture2).



Option Explicit

Private img As StdPicture
Private factor As Double
Private startX As Single
Private startY As Single


Private Sub Form_Load()
    Me.Caption = "Pan and Zoom PictureBox Example"
    Me.ScaleMode = vbPixels
   
    Command1.Caption = "Load Img"
    Command2.Caption = "Zoom In"
    Command3.Caption = "Zoom Out"
    Command4.Caption = "Reset"
   
    Picture1.Appearance = 0 ' flat
    Picture1.BorderStyle = 0 ' none
    Picture1.ScaleMode = vbPixels
    Picture1.BackColor = vbBlack
   
    Picture2.Appearance = 0 ' flat
    Picture2.BorderStyle = 0 ' none
    Picture2.AutoRedraw = True
    Picture2.ScaleMode = vbPixels
    Picture2.Move 0, 0, 0, 0
End Sub

Private Sub Command1_Click()
    CommonDialog1.CancelError = True
    CommonDialog1.DialogTitle = "Select an Image to Load"
    CommonDialog1.Filter = "Images (*.bmp;*.jpg;*.gif;*.ico)|*.bmp;*.jpg;*.gif;*.ico"
    CommonDialog1.ShowOpen
    LoadImage CommonDialog1.fileName
cancelled:
End Sub

Private Sub LoadImage(ByVal fileName As String)
    On Error GoTo BadImage
   
    factor = 1#
    Picture2.Move 0, 0, 0, 0
    Set img = Nothing
    Set img = LoadPicture(fileName)
    PaintImage
    Exit Sub
   
BadImage:
    MsgBox fileName, vbCritical, "Invalid Image"
End Sub

Private Sub Command2_Click()
    If Not (img Is Nothing) Then
        factor = factor + 0.1
        Command3.Enabled = True
        PaintImage
    End If
End Sub

Private Sub Command3_Click()
    If Not (img Is Nothing) Then
        If factor > 0.1 Then
            factor = factor - 0.1
            If factor < 0.1 Then
                factor = 0.1
            End If
            PaintImage
        End If
        If factor <= 0.1 Then
            Command3.Enabled = False
        End If
    End If
End Sub

Private Sub Command4_Click()
    factor = 1#
    Picture2.Move 0, 0
    PaintImage
End Sub

Private Sub PaintImage()
    If Not (img Is Nothing) Then
        Picture2.Cls
        Picture2.Width = Picture1.ScaleX(img.Width, vbHimetric, vbPixels) * factor
        Picture2.Height = Picture1.ScaleY(img.Height, vbHimetric, vbPixels) * factor
        Picture2.PaintPicture img, 0, 0, Picture2.Width, Picture2.Height
        Label1.Caption = Format(factor * 100, "0") & "%" & " @ " & Format(Picture2.Width, "0") & "x" & Format(Picture2.Height, "0")
   
        If Picture2.Left + Picture2.Width < Picture1.Width Then
            Picture2.Left = Picture1.Width - Picture2.Width
        End If
        If Picture2.Top + Picture2.Height < Picture1.Height Then
            Picture2.Top = Picture1.Height - Picture2.Height
        End If
   
        If (Picture2.Left > 0) Or (Picture2.Width < Picture1.Width) Then
            Picture2.Left = 0
        End If
        If (Picture2.Top > 0) Or (Picture2.Height < Picture1.Height) Then
            Picture2.Top = 0
        End If
    End If
End Sub

Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        startX = X
        startY = Y
    End If
End Sub

Private Sub Picture2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        Dim newX As Single
        Dim newY As Single
       
        newX = Picture2.Left + (X - startX)
        newY = Picture2.Top + (Y - startY)
       
        If newX < 0 Then
            If Picture2.Width > Picture1.Width Then
                If newX + Picture2.Width < Picture1.Width Then
                    newX = -(Picture2.Width - Picture1.Width)
                End If
                Picture2.Move newX, Picture2.Top
            End If
        End If
       
        If newY < 0 Then
            If Picture2.Height > Picture1.Height Then
                If newY + Picture2.Height < Picture1.Height Then
                    newY = -(Picture2.Height - Picture1.Height)
                End If
                Picture2.Move Picture2.Left, newY
            End If
        End If
    End If
End Sub
0
 
eneateAuthor Commented:
Thanks that works great,
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now