Hello, a complete Newbie question.
I have created a windows form with a picture box and loaded an image.
I would really like to use a trackbar or something to zoom in on the picture and if necessary automatically create scrollbars.
But I am not sure where to start with this.
Can anyone give me a pointer in very simple terms
Here's a general example on one way to do it (although I am not saying this is the best way, just one way). Simply gets the image, and makes it bigger by 50 on the height and width of the original image....
Code:
Dim myimage As Image = PictureBox1.Image 'original picturebox image
Dim BiggerImage As Bitmap
BiggerImage = New Bitmap(myimage, myimage.Width + 50, myimage.Height + 50)
PictureBox1.Image = BiggerImage 'new larger image
You could incorprate this into a slider or something, where the increased height and width is a percentage of the width and height of the original image. Also, you would probably want to keep the original image in a public image object or something so you can always resort back to the original one, I would assume.
One issue you will probably have is how to make the picturebox "scroll" so you can see all of the larger image, which I do not have an answer for (yet), unless you resize the picturebox to fit the larger image on each zoom....
Last edited by gigemboy; Feb 1st, 2006 at 11:58 AM.
To solve the scrolling problem, put your picturebox inside of a Panel Control, and set the Panel control to AutoScroll. That way, when the picturebox is resized, you will be able to scroll when the image reaches past the bounds of the panel...
Code:
'make sure the picturebox is inside of a panel control, then try this...
Dim myimage As Image = PictureBox1.Image
Dim BiggerImage As Bitmap
BiggerImage = New Bitmap(myimage, myimage.Width + 50, myimage.Height + 50)
PictureBox1.Height = BiggerImage.Height
PictureBox1.Width = BiggerImage.Width
PictureBox1.Image = BiggerImage
A cleaner way would be to inherit a picturebox and paint the picture with the apropriate zoom.
I was working on something similar in C# it is not finished but I'll gladly show it to you. But you would have to understand C#.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
I'd be interested in your C# version, I've been looking for something like this.
Thanks
Using VB.NET 2003/.NET 1.1/C# 2.0 http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
Here it is, you set the zoom level with the ZoomLevel property (1 = 100%)
It also lets you drag the Image around.
edit: I created it with VS2005 but as far as I can see the code should work in 2003.
Last edited by grilkip; Feb 1st, 2006 at 12:38 PM.
"so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman
Well here is using my way... Put a slider control on your form, and a picturebox inside of a panel control, set the picturebox image to something in designer, change the slider control properties to "SmallChange" = 25, "TickFrequency" = 50, "LargeChange" = 50, "Maximum" = 500, "Minimum" = 100, "Value" = 100 . Then put the following code in the form...
Code:
Private OriginalImage As Image
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
TextBox1.Text = TrackBar1.Value.ToString
Dim BiggerImage As Bitmap
BiggerImage = New Bitmap(OriginalImage, Convert.ToInt32(OriginalImage.Width * (Convert.ToInt32(TextBox1.Text) / 100)), Convert.ToInt32(OriginalImage.Height * (Convert.ToInt32(TextBox1.Text) / 100)))
PictureBox1.Height = BiggerImage.Height
PictureBox1.Width = BiggerImage.Width
PictureBox1.Image = BiggerImage
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Size = PictureBox1.Image.Size
OriginalImage = PictureBox1.Image
End Sub
Using VB.NET 2003/.NET 1.1/C# 2.0 http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
I have now done this and it works great...well almost.
My picturebox is on a panel. The image is set to stretch. I have set a trackbar to zoom in on the picture. Interestingly even though I have autoscroll turned on on the panel, the scroll bars don't appear when i enlarge the image. So anyway I put two scroll bars on the panel from the toolbox and they are working OK.
You can imagine after some zooming and scrolliing the picture becomes a little difficult to manage so I decided to put a "Reset" button on my form. The reset button works nicely except that after I press it I have to do a quick click on the scrollbar to view the picture, it doesn't automatically take me back to where I started. Can anyone help me to work out how to set it back to how it was before zooming (maybe some kind of refresh? or I have heard of resizeredraw but I don't know what that does)
Thanks a lot.
The code for the click on my "Reset" button is below
VB Code:
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click