none
drag large picture RRS feed

  • Question

  • Hi, I have written a fully-working app (in VB6) with no probs. Up and running. Now I want to 'translate' the app into vb.net, most of which I have done - but the following is giving me problems since I have only just started vb.net and am not an expert!

    I have a static picture box, about 500 x 700 pixels, on the LHS of a form, with another inside it into which I load a graphic. The graphic is always 1200 * 1500 pixels so only about a quarter of it will be visible at any one time. In the VB6 version I can drag the inner picbox around 'inside' the static one, in order to bring previously-hidden regions into view with the static box acting as a 'viewport' into the large picture. But, because the various arguments and subs are very different in vb.net I cannot figure out how to accomplish this.

    Help!

    Please could you address any replies to mike 'at' starman dot co dot uk as that is the account I check most often, thanks.



    Friday, January 5, 2018 11:53 PM

All replies

  • You can do it the same way with the proper code in vb.net. However there are most likely easier ways now. How to do it depends.

    First off you can draw the visible part of the image based on mouse movement or scrollbars or whatever. Now its is much easier to draw in .net.

    You can also use a control in a control where the inside control is larger than the outside one. Move the inside control while the outside remains stationary.

    So did you want to draw your own or use a picturebox inside a picturebox?

    What are you viewing exactly? A map? A photograph?

    Plus do you want to move the viewport on the screen with the picture stationary or move the picture with the screen viewport stationary?

    Saturday, January 6, 2018 12:29 AM
  •  Here is a small example that allows you to drag an image around in a PictureBox.  However,  notice that the Image is not actually assigned to the PictureBox's Image property.  Instead,  it is assigned to a class scoped Bitmap which is being drawn on the PictureBox in the PictureBox's Paint event.

     From there it is just the MouseDown and MouseMove events of the PictureBox that are used to adjust the Location of where the image is drawn in the PictureBox Paint event.

    Public Class Form1
        Private Map As New Bitmap("C:\TestFolder\USMap.png") 'Image size is 4726x2960
        Private MapLocation As Point = Point.Empty
        Private MouseDownPoint As Point = Point.Empty
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            If e.Button = MouseButtons.Left Then
                MouseDownPoint = e.Location
            End If
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If e.Button = MouseButtons.Left Then
                MapLocation.X -= (MouseDownPoint.X - e.X)
                MapLocation.Y -= (MouseDownPoint.Y - e.Y)
                MouseDownPoint = e.Location
                PictureBox1.Invalidate()
            End If
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.DrawImage(Map, MapLocation.X, MapLocation.Y, Map.Width, Map.Height)
        End Sub
    End Class
    

     

     If you want to keep the image from being dragged further than the edges of the image,  you can change the Paint event code like show below.  You will also want to use the PictureBox's Resize event to make it repaint the image.  This is so that if the image was dragged all the way left and you resized the PictureBox larger in any way,  it will keep the image drawn so the right edge is all the way to the right of the PictureBox.

        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            'Add these 4 lines in the paint event
            If MapLocation.X > 0 Then MapLocation.X = 0
            If MapLocation.X < -(Map.Width - PictureBox1.Width) Then MapLocation.X = -(Map.Width - PictureBox1.Width)
            If MapLocation.Y > 0 Then MapLocation.Y = 0
            If MapLocation.Y < -(Map.Height - PictureBox1.Height) Then MapLocation.Y = -(Map.Height - PictureBox1.Height)
    
            e.Graphics.DrawImage(Map, MapLocation.X, MapLocation.Y, Map.Width, Map.Height)
        End Sub
    
        'add the resize event
        Private Sub PictureBox1_Resize(sender As Object, e As EventArgs) Handles PictureBox1.Resize
            PictureBox1.Invalidate()
        End Sub
    

     

     


    If you say it can`t be done then i`ll try it

    Saturday, January 6, 2018 2:50 AM