Results 1 to 10 of 10

Thread: How Can I zoom on a picturebox

  1. #1

    Thread Starter
    Lively Member Iain Wicks's Avatar
    Join Date
    Jun 2005
    Location
    Tring, Hertfordshire
    Posts
    88

    How Can I zoom on a picturebox

    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

    thanks

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How Can I zoom on a picturebox

    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.

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How Can I zoom on a picturebox

    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

  4. #4
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: How Can I zoom on a picturebox

    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

  5. #5
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: How Can I zoom on a picturebox

    Hi grilkip

    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 ...

  6. #6
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: How Can I zoom on a picturebox

    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.
    Attached Files Attached Files
    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

  7. #7
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How Can I zoom on a picturebox

    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
    Did this quick because I have to leave for a few

  8. #8
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Re: How Can I zoom on a picturebox

    Thanks grilkip
    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 ...

  9. #9
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: How Can I zoom on a picturebox

    inherited panel, implemented autoscroll, zoom factor, autofit.

    the more the merrier...
    Attached Files Attached Files

  10. #10

    Thread Starter
    Lively Member Iain Wicks's Avatar
    Join Date
    Jun 2005
    Location
    Tring, Hertfordshire
    Posts
    88

    Re: How Can I zoom on a picturebox

    Thanks a lot everyone.

    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:
    1. Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    2.         PicImageSingle.Height = 696
    3.         PicImageSingle.Width = 696
    4.         Panel1.Height = 700
    5.         Panel1.Width = 700
    6.         vscr.Value = 0
    7.         hscr.Value = 0
    8.         TrackBar1.Value = 696
    9.         lblpixels.Text = String.Format("Pixel value: {0} ", TrackBar1.Value.ToString)
    10.     End Sub

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