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?
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.
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.
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?
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!
Re: How do you move the position of a background image inside a PictureBox?
Quote:
Originally Posted by
dunfiddlin
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:bigyello:.
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
Re: How do you move the position of a background image inside a PictureBox?
BB, you rock! Thanks so much... works like a charm :)
Re: How do you move the position of a background image inside a PictureBox?
Quote:
If dunfiddlin says something is impossible, it's practically a guarantee that it's not hard to do.
It's a gift, you know! :(