0

I have an image with 10,000px by 8,000px with a drag method. the problem is that whenever I zoom it in/out or drag it the image flickers. how can I avoid this problem?

I tried resizing into smaller one, but the image becomes pixelated and I cant read anymore the texts in it.

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

    'PictureBox1.CreateGraphics.DrawLine(Pens.Cyan, e.Location.X, e.Location.Y, e.X, e.Y + 100)
    ' PictureBox1.CreateGraphics.DrawEllipse(Pens.Cyan, New Rectangle(e.Location.X - 7.5, e.Location.Y - 7.5, 15, 15))

    If e.Button = Windows.Forms.MouseButtons.Left Then
        mouseDowns = e.Location
    End If
    LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

    Label1.Text = TranslateImageCoordinate(LocalMousePosition).X.ToString
    Label2.Text = TranslateImageCoordinate(LocalMousePosition).Y.ToString
    Label3.Text = WhereIAm(TranslateImageCoordinate(LocalMousePosition))

    If WhereIAm(TranslateImageCoordinate(LocalMousePosition)) = "" Then
        TextBox1.Focus()
    End If

End Sub

Private Function TranslateImageCoordinate(ByVal coordinate As Point) As Point

    Dim ratioWidth, ratioHeigth As Double
    ratioWidth = PictureBox1.Image.Size.Width / PictureBox1.Size.Width
    ratioHeigth = PictureBox1.Image.Size.Height / PictureBox1.Size.Height
    Dim newXc As Double
    Dim newYc As Double

    newXc = ratioWidth * coordinate.X
    newYc = ratioHeigth * coordinate.Y

    Return New Point(newXc, newYc)

End Function

Private Function reverseImageCoordinate(ByVal coordinate As Point) As Point
    Dim ratioWidth, ratioHeigth As Double
    ratioWidth = PictureBox1.Image.Size.Width / PictureBox1.Size.Width
    ratioHeigth = PictureBox1.Image.Size.Height / PictureBox1.Size.Height
    Dim newXc As Double
    Dim newYc As Double

    newXc = coordinate.X / ratioWidth
    newYc = coordinate.Y / ratioHeigth

    Return New Point(newXc, newYc)
End Function

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

    LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)
    '========================PRINT IMAGE COORDINATE=======================================
    ' Label1.Text = TranslateImageCoordinate(LocalMousePosition).X
    ' Label2.Text = TranslateImageCoordinate(LocalMousePosition).Y
    '=====================================================================================
    Dim mouse As MouseEventArgs = e

    If (mouse.Button = Windows.Forms.MouseButtons.Left) Then

        Dim mousePosNow As Point = mouse.Location

        Dim deltaX As Integer = mousePosNow.X - mouseDowns.X
        Dim deltaY As Integer = mousePosNow.Y - mouseDowns.Y

        Dim newX As Integer = Clamp(PictureBox1.Location.X + deltaX, PictureBox1.Width, Panel1.Width)
        Dim newY As Integer = Clamp(PictureBox1.Location.Y + deltaY, PictureBox1.Height, Panel1.Height)

        PictureBox1.Location = New Point(newX, newY)
    End If

End Sub

Private Function Clamp(ByVal val As Integer, ByVal outerBound As Integer, ByVal innerBound As Integer) As Integer
    Dim newVal As Integer = val

    If newVal > 0 Then
        newVal = 0
    End If

    If newVal + outerBound < innerBound Then
        newVal = innerBound - outerBound
    End If

    Return newVal

End Function

Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel

    Dim newWidth As Integer = PictureBox1.Image.Size.Width, _
    newHeight = PictureBox1.Image.Size.Height, _
    newX = PictureBox1.Location.X, _
    newY = PictureBox1.Location.Y

    If e.Delta > 0 Then

        newWidth = PictureBox1.Size.Width + (PictureBox1.Size.Width / 10)
        newHeight = PictureBox1.Size.Width + (PictureBox1.Size.Height / 10)
        newX = PictureBox1.Location.X - ((PictureBox1.Size.Width / 10) / 2)
        newY = PictureBox1.Location.Y - ((PictureBox1.Size.Height / 10) / 2)

    ElseIf e.Delta < 0 Then

        newWidth = PictureBox1.Size.Width - (PictureBox1.Size.Width / 10)
        newHeight = PictureBox1.Size.Width - (PictureBox1.Size.Height / 10)
        newX = PictureBox1.Location.X + ((PictureBox1.Size.Width / 10) / 2)
        newY = PictureBox1.Location.Y + ((PictureBox1.Size.Height / 10) / 2)

        If (newWidth < Panel1.Width Or newHeight < Panel1.Height) Then
            newWidth = Panel1.Width
            newHeight = Panel1.Height
            newX = Panel1.Location.X
            newY = Panel1.Location.Y
        End If
        If newX > Panel1.Location.X Then
            newX = 0
        End If
        If newY > Panel1.Location.Y Then
            newY = 0
        End If
        If (newX + newWidth) < (Panel1.Location.X + Panel1.Width) Then
            newX = (Panel1.Location.X + Panel1.Width) - PictureBox1.Width
        End If
        If (newY + newHeight) < (Panel1.Location.Y + Panel1.Height) Then
            newY = (Panel1.Location.Y + Panel1.Height) - PictureBox1.Height
        End If
    End If

    PictureBox1.Hide()
    PictureBox1.Size = New Size(newWidth, newHeight)
    PictureBox1.Location = New Point(newX, newY)
    PictureBox1.Show()
    Label3.Text = zoomCounter

End Sub

the code up there is the zoom in/zoom out and panning.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
jko
  • 133
  • 1
  • 7
  • 14
  • 1
    Couple things here that might help. First, what language are you working with? You say "Visual Basic" but do you mean VB6, VB.NET 3.5, VB.NET 4.0, etc.? Second, what type of application is this? For example, Winforms, WPF, Silverlight, ASP.NET, etc.? Third, is there a reason why you are dealing with such a large image? How did you try to shrink it (did you just tell the system to make it smaller or did you actually resize it properly - it shouldn't have become pixelated in getting smaller)? Fourth, can your system handle the large picture (CPU/RAM for Winforms, GPU for WPF)? – IAmTimCorey Jul 25 '11 at 04:56
  • The basic point is that we need more information to help you out intelligently. It helps us more easily pinpoint problems. That is why we like to see code. Then there is no confusion (usually) about what is happening. One final point is you should clean up your tags to reflect what you are doing. You have one tag for "visual" and another for "basic". These should be one tag, and specific to the type (VB or VB.NET). – IAmTimCorey Jul 25 '11 at 04:58
  • sorry for the confusion, I am using Visual Basic.net 3.5 in windows forms. The reason for its large image is that it is a map in our place and I am implementing a shortest path in it. I shrink it using Adobe Photoshop but when I zoomed it in, it is blurred. I think my ram/cpu(4gb/2.2core2duo) can handle it because when I opened the image in photoshop, it dont lag at all even when I drag it. Sorry for missed tag (visual basic) because they dont make me try to tag it using "Visual Basic.Net" due to some reason, so I just used "Visual Basic" instead. – jko Jul 25 '11 at 05:12

0 Answers0