Results 1 to 8 of 8

Thread: How do you move the position of a background image inside a PictureBox?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    How do you move the position of a background image inside a PictureBox?

    Hey guys,

    I have a PictureBox control that will display an image that the end user specifies, and then position this image in the very center of the PictureBox (as it's background image). The width & height of these background images will vary, but they will always be smaller than the PictureBox.

    What I would like to do is... each time that the user clicks button1, the background image position should move down 1 pixel inside the PictureBox. But the PictureBox itself should not move (otherwise I could solve this problem simply by adjust the PictureBox's top-margin inside it's parent container).

    For example... I know that there is not a "top" property specifically for a PictureBox's background image, but if there was, then I would want to do something like this...

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If PictureBox1.BackgroundImage IsNot Nothing Then
    
                PictureBox1.BackgroundImage.Top += 1
    
            End If
    
        End Sub
    So... any ideas how to accomplish something like this?

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How do you move the position of a background image inside a PictureBox?

    Here's a way to do it by modifying the background image itself.

    Code:
            Private pbTop As Integer = 0
    	Private pbImage As New Bitmap(image filename)
    
    	Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    		pbTop += 1
    		Dim bmp As New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
    		Using g As Graphics = Graphics.FromImage(bmp)
    			'g.Clear(PictureBox1.BackColor)
    			g.DrawImage(pbImage, 0, pbTop, bmp.Width, bmp.Height)
    		End Using
    		PictureBox1.BackgroundImage = bmp
    	End Sub
    You'll have to keep a clean copy of the original background image somewhere if you want to move it up as well as down. You could keep a copy in your program resources, or read it into a variable as in the example above.

    BB

    EDIT: paulg4ije (next post) is right: the Clear instruction is unnecessary because the new bitmap is transparent.

  3. #3
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: How do you move the position of a background image inside a PictureBox?

    One solution would be to draw your loaded image on to a bitmap at the desired location, then assign that bitmap to the picturebox. Something like this:

    Code:
    Dim bm As New Bitmap("c:\mypic.bmp")
    Dim bm2 As New Bitmap(picturebox1.width, picturebox1.height)
    Dim g As Graphics = Graphics.FromImage(bm)
    g.DrawImage(bm2, X, Y)
    picturebox1.image = bm2
    Don't take that code too literally, but it should be something along those lines. If you Google Graphics.FromImage you should get lots of good stuff.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Re: How do you move the position of a background image inside a PictureBox?

    Thanks guys.

    @ BB... I tried your code, but when I click the button, all it does is expand the background image to where it fills up the entire PictureBox. It's exactly as if it changed the BackGround Image Layout to "stretch" (but I have it set to "center").

    Any ideas?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How do you move the position of a background image inside a PictureBox?

    If you've got it set to Center then nothing's going to work because the setting will override everything and return the image to the center anyway!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How do you move the position of a background image inside a PictureBox?

    Quote Originally Posted by dunfiddlin View Post
    If you've got it set to Center then nothing's going to work because the setting will override everything and return the image to the center anyway!
    That's good news. If dunfiddlin says something is impossible, it's practically a guarantee that it's not hard to do.

    Here's a version of the code for a centred image. I've marked the changed lines red:
    Code:
            Private pbTop As Integer = 0
    	Private pbImage As New Bitmap(image filename)
    
    	Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    		pbTop += 1
    		Dim bmp As New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
    		Dim x As Integer = bmp.Width \ 2 - pbImage.Width \ 2
    		Dim y As Integer = bmp.Height \ 2 - pbImage.Height \ 2
    		Using g As Graphics = Graphics.FromImage(bmp)
    			g.DrawImage(pbImage, x, y + pbTop)
    		End Using
    		PictureBox1.BackgroundImage = bmp
    	End Sub
    BB

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    126

    Re: How do you move the position of a background image inside a PictureBox?

    BB, you rock! Thanks so much... works like a charm

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How do you move the position of a background image inside a PictureBox?

    If dunfiddlin says something is impossible, it's practically a guarantee that it's not hard to do.
    It's a gift, you know!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width