|
-
Dec 8th, 2012, 02:50 PM
#1
Thread Starter
Lively Member
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?
-
Dec 8th, 2012, 03:54 PM
#2
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.
Last edited by boops boops; Dec 8th, 2012 at 04:29 PM.
-
Dec 8th, 2012, 04:01 PM
#3
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.
-
Dec 8th, 2012, 04:40 PM
#4
Thread Starter
Lively Member
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?
-
Dec 8th, 2012, 04:55 PM
#5
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!
-
Dec 8th, 2012, 07:46 PM
#6
Re: How do you move the position of a background image inside a PictureBox?
 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 .
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
-
Dec 8th, 2012, 09:05 PM
#7
Thread Starter
Lively Member
Re: How do you move the position of a background image inside a PictureBox?
BB, you rock! Thanks so much... works like a charm
-
Dec 9th, 2012, 11:41 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|