Zoom in & out of an Image within a PictureBox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: Zoom in & out of an Image within a PictureBox

  1. #1
    Join Date
    Jun 2006
    Posts
    194

    Question Zoom in & out of an Image within a PictureBox

    I have an image in a PictureBox and I want to be able to have buttons on my form that allow a user to zoom in and out as they wish. However, it doesn't seem like the .Zoom property works with the PictureBox. Is there another way around in this? Maybe a way to scale the PictureBox according to the same way .Zoom would work? Here's my code, but it did not zoom in or out:

    Code:
    Public Const ZOOM_MAX As Double = 25600
    Public Const ZOOM_MIN As Double = 0.1
    
    Private Sub mnuViewZoomIn_Click()
       On Error GoTo ErrHandle
       pic.Picture.Zoom = 2 * pic.Picture.Zoom
       tbToolBar.Buttons("ZoomOut").Enabled = True
       If pic.Picture.Zoom >= ZOOM_MAX Then
          tbToolBar.Buttons("ZoomIn").Enabled = False
       End If
       Exit Sub
       
    ErrHandle:
       tbToolBar.Buttons("ZoomIn").Enabled = False
    End Sub
    
    Private Sub mnuViewZoomout_Click()
       On Error GoTo ErrHandle
       pic.Picture.Zoom = 0.5 * pic.Picture.Zoom
       tbToolBar.Buttons("ZoomIn").Enabled = True
       If pic.Picture.Zoom <= ZOOM_MIN Then
          tbToolBar.Buttons("ZoomOut").Enabled = False
       End If
       Exit Sub
       
    ErrHandle:
       tbToolBar.Buttons("ZoomOut").Enabled = False
    End Sub

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Zoom in & out of an Image within a PictureBox

    I'd recommend using the BitBlt and StretchBlt API's.

    Have a look at the attachment. It basically covers your "Zoom In" part...
    Attached Files Attached Files
    Last edited by HanneSThEGreaT; January 16th, 2007 at 10:28 AM.

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,723

    Re: Zoom in & out of an Image within a PictureBox

    That little thing is really ingenious.

  4. #4
    Join Date
    Jun 2006
    Posts
    194

    Re: Zoom in & out of an Image within a PictureBox

    HanneSThEGreaT, that is a very clever program. Thank you! I'm trying to adapt something like that in my program; however, I'm having trouble. I have one PictureBox containing an image like your Picture1, but I need that full image & PictureBox to zoom in or out rather than having a second PictureBox that shows the zoom (Picture3 in your program). I can move Mouse events into my Zoom Button click functions, but my problem is adapting to using only one PictureBox. Any guidance in the right direction is much appreciated.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Zoom in & out of an Image within a PictureBox

    Hmmm the picturebox control does not even have a zoom property..
    Zoom applies only to the printers control's
    Quote Originally Posted by VB Books Online
    Applies To
    Printer Object, Printers Collection

    Returns or sets the percentage by which printed output is to be scaled up or down. Not available at design time.

    Syntax
    object.Zoom [= number]
    Hannes, that is smart code.

    Slowcoder, to finally solve the problem we need to look at posibly passing the image in a hidden picturebox to StretchBlt... Hmm where's Dan when you need him ???
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Zoom in & out of an Image within a PictureBox

    Thanx guys, you made my day!
    I'll second the idea of making use of g hidden picturebox.
    I'm busy with another option, slowcoder, I'll post it tomorrow.
    Quote Originally Posted by GremlinSA
    Hmm where's Dan when you need him ???
    Yeah, Dan, where are you,

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Zoom in & out of an Image within a PictureBox

    OK, I've worked on something last night (this morning)..

    It makes use of one picturebox, and Image control, and allows for shrinking and enlarging. It is a bit flickery though...
    Attached Files Attached Files

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Zoom in & out of an Image within a PictureBox

    Very good work Hannes.. I dont want to know how you worked out the ratio's to use but it was good..

    Well now i took it and expanded of the matematics a little so now you can adjust the immage anywhere between 25% and 1000$ (1/4 size up to 10 times)

    I also added a timer on the enlarge and shrink to slow it down a little. Have a go..

    Gremmy..
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Zoom in & out of an Image within a PictureBox

    WOW! NICE! Very, Very Nicely done!.

    I like the idea of having the Image_Pos Type, it makes the whole "managing" of the picture very easy.


    PS: Thanx for still keeping my ac/dc pic

  10. #10
    Join Date
    Jun 2006
    Posts
    194

    Re: Zoom in & out of an Image within a PictureBox

    WOW! HanneSThEGreaT & GremlinSA...your projects are great! And Gremlin, that scrollbar is a perfect addition to the zoom feature I'm going to try and adapt these concepts into my project today; however, I noticed that there's an Image control used....and I think I goofed in my earlier post by not saying that my image is actually a picture bound to the picturebox: pictureBox1.Picture = LoadPicture(TempFileName)

    I definitely need to be more careful with my terminology. So I'm going to try and change my project to use an image control instead, but we'll see if I break any other code in the process...

  11. #11
    Join Date
    Aug 2006
    Posts
    145

    Re: Zoom in & out of an Image within a PictureBox

    my 2 cents, replace
    Code:
    imgZoom.Visible = False
    imgZoom.Top = Tmp_Pos.Top
    imgZoom.Left = Tmp_Pos.Left
    imgZoom.Height = Tmp_Pos.Height
    imgZoom.Width = Tmp_Pos.Width
    imgZoom.Visible = True
    with
    Code:
    imgZoom.Move Tmp_Pos.Left, Tmp_Pos.Top, Tmp_Pos.Width, Tmp_Pos.Height
    and most of the flickering will disappear.

  12. #12
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,723

    Re: Zoom in & out of an Image within a PictureBox

    Cannot help but applauding, Hannes and Gremlin.

    Shame that I still have to "spread reputation around" before I can come back to you.

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,098

    Re: Zoom in & out of an Image within a PictureBox

    Quote Originally Posted by bushmobile
    and most of the flickering will disappear.
    That's actually a bit more than 2cents worth . Thanx, I was concerned about the flickering!

    Quote Originally Posted by WoF
    Cannot help but applauding, Hannes and Gremlin.

    Shame that I still have to "spread reputation around" before I can come back to you.
    Thanx man, it's always amazing how a simple idea can spark so many other ideas, and how more than one head always seem to work

    Credit definitely goes to Gremmy on this one....

  14. #14
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Zoom in & out of an Image within a PictureBox

    Hey this is a clasic case of a Group of Guru's each adding in there own special methods to a unified cause.

    credit goes out too all on this one..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  15. #15
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Zoom in & out of an Image within a PictureBox

    Quote Originally Posted by slowcoder
    WOW! HanneSThEGreaT & GremlinSA...your projects are great! And Gremlin, that scrollbar is a perfect addition to the zoom feature I'm going to try and adapt these concepts into my project today; however, I noticed that there's an Image control used....and I think I goofed in my earlier post by not saying that my image is actually a picture bound to the picturebox: pictureBox1.Picture = LoadPicture(TempFileName)

    I definitely need to be more careful with my terminology. So I'm going to try and change my project to use an image control instead, but we'll see if I break any other code in the process...
    Are you trying to add this to the crop image app we worked on before..

    I had an idea about it...

    using a Rouge hidden Imagebox... leave the picturbox in place.. when doing a zoom, put the pic in the Imagebox. Zoom it to the right size. Place it over the Picbox in the right possition. Make it visible. BitBlt it into the Picture box, then hide it again..

    Look it's going to be alot of code, and right now the wife want a bit of time online, so i wont be able to look at it right now.. but i will look into doing something a bit later..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center