ZoomPictureBox: picture control with mouse-centred zooming - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 56 of 56

Thread: ZoomPictureBox: picture control with mouse-centred zooming

  1. #41

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,382

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hi jfrazier, welcome to the forums.

    You can code the Click event subs of the buttons to change the ZoomFactor property. You could add/subtract to the present zoom factor or multiply/divide it; that's a matter of your own design. Alternatively you could use a number of buttons each to set a specific value of the ZoomFactor (1 means original image size). As an example, suppose you add a "Zoom In" button to the form. Then double click the button and put this in its Click event sub (assuming the ZoomPictureBox is named zpb1):
    Code:
    zpb1.ZoomFactor * = 1.25
    And you could put something similar but with a factor of say 0.75 in the "Zoom Out" button's Click sub.

    The scroll wheel zooming will still work as long as you don't change EnableMouseWheelZooming to False. If you are using ZoomMode "zoom to mouse position", that might look a little odd when you click a button to zoom. But it's easy to change the mode to CenterControl temporarily in the button's Click sub, for example:

    Code:
    	zpb1.ZoomMode = ZPBLibrary.ZoomPictureBox.ZoomType.ControlCenter
    	zpb1.ZoomFactor *= 1.25
    	zpb1.ZoomMode = ZPBLibrary.ZoomPictureBox.ZoomType.MousePosition
    BB

  2. #42
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Thank you. That was much more simple than what I was trying unsuccesfully to do. I was turning off the mouse control and then turning it back on, or at least that was what I was trying to do. It wasn't working in the least, but this works like a charm.

    Many thanks from a .Net Newbie

  3. #43
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    I am trying to add zoom features to my program and I am doing a fit to width and fit to height click options. I am getting the size working, but I am having no luck on moving the picture within the zoompicturebox on just the click. I am attempting to get it to the upper left corner. My research is showing that the picturebox would use sizemode, but as I am not inheriting from picturebox it is not present with zoompicturebox. So far I have not found a combination that does what I am trying to do. Any ideas?

    Thank you,
    newbie

  4. #44

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,382

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Try this:
    Code:
    ZoomPictureBox1.ImageLocation = Point.Empty
    Point.Empty is the same as Point(0,0). With a little bit of arithmetic with the widths and heights you should be able to work out how to centre the image if you need to.

    BB

  5. #45
    New Member
    Join Date
    Jun 2013
    Posts
    3

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    What would keep the ZoomFactor property from updating? When I load a new image into the ZoomPictureBox, I check the size of the control vs. the size of the image and depending on the aspect ratio I attempt to set the ZoomFactor to size the image so it is fit inside the control. I attempt to use zoompicturebox.ZoomFactor = <some value> but the value is not updating. For example:

    Dim wRatio As Double = splContainer.Panel2.Width / zpbImage.Image.Width
    Dim hRatio As Double = splContainer.Panel2.Height / zpbImage.Image.Height
    If wRatio < hRatio Then zpbImage.ZoomFactor = wRatio Else zpbImage.ZoomFactor = hRatio

  6. #46
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    try zpbImage.Refresh() after what you have.

  7. #47
    New Member
    Join Date
    Jun 2013
    Posts
    3

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Quote Originally Posted by jfrazier View Post
    try zpbImage.Refresh() after what you have.
    Sorry, I did have the refresh method in there, it somehow got cut off when I pasted the code in. What is odd is that it works early on but after loading some images, resizing the form, or zooming in it will exhibit this behavior. I'm sure I'm messing something up in my other code but I can't figure out what it might be.

    This is an awesome control and is working really well for me except for this one little issue.

  8. #48
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    I have had to rig up a zoom of approximately 200%. I'm still trying to get a firm grasp on the zoom factor property.

  9. #49

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,382

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Quote Originally Posted by fryed View Post
    What would keep the ZoomFactor property from updating? When I load a new image into the ZoomPictureBox, I check the size of the control vs. the size of the image and depending on the aspect ratio I attempt to set the ZoomFactor to size the image so it is fit inside the control. I attempt to use zoompicturebox.ZoomFactor = <some value> but the value is not updating.
    The ZoomPictureBox does that automatically when you set its Image property. So you shouldn't have to do anything at all.

    For example:

    Dim wRatio As Double = splContainer.Panel2.Width / zpbImage.Image.Width
    Dim hRatio As Double = splContainer.Panel2.Height / zpbImage.Image.Height
    If wRatio < hRatio Then zpbImage.ZoomFactor = wRatio Else zpbImage.ZoomFactor = hRatio
    I'm not sure how that works because I don't know what you are doing with splContainer.Panel2. But I suspect something is wrong with the logic. To fit an image to a box, what you need to do is this: If the picture is wider and flatter than the box, fit the image widthways. Otherwise fit it heightways. That's all! If you want a code example, look in the ZoomPictureBox code for a function called FitImage (or something like that).

    BB

  10. #50
    New Member
    Join Date
    Jun 2013
    Posts
    3

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Quote Originally Posted by boops boops View Post
    The ZoomPictureBox does that automatically when you set its Image property. So you shouldn't have to do anything at all.

    I'm not sure how that works because I don't know what you are doing with splContainer.Panel2. But I suspect something is wrong with the logic. To fit an image to a box, what you need to do is this: If the picture is wider and flatter than the box, fit the image widthways. Otherwise fit it heightways. That's all! If you want a code example, look in the ZoomPictureBox code for a function called FitImage (or something like that).

    BB
    I made a simple test project with just a couple lines to add an image to the control and as you said, it fits it automatically without me having to do anything else. I must've overthought it when writing my code and buggered something up in the process. I'll have to go back and redo my stuff and simplify it. Thanks for your help.

    Dan

  11. #51
    New Member
    Join Date
    Sep 2013
    Posts
    2

    Lightbulb Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hello
    Regarding the mouse wheel event, I would suggest you change the way you "grab" focus to something more elegant, in the same way Outlook or Internet Explorer works.

    You do this by first implementing the IMessageFilter interface like this:

    Code:
    Public Class ZoomPictureBox
        Inherits UserControl
        Implements IMessageFilter
    Then you add this line in your constructor:

    Code:
    Application.AddMessageFilter(Me)
    And the following method and declarations:

    Code:
        Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
            If m.Msg = &H20A Then
                ' WM_MOUSEWHEEL, find the control at screen position m.LParam
                Dim pos As New Point(m.LParam.ToInt32() And &HFFFF, m.LParam.ToInt32() >> 16)
                Dim hWnd As IntPtr = WindowFromPoint(pos)
                If hWnd <> IntPtr.Zero AndAlso hWnd <> m.HWnd AndAlso Control.FromHandle(hWnd) IsNot Nothing Then
                    SendMessage(hWnd, m.Msg, m.WParam, m.LParam)
                    Return True
                End If
            End If
            Return False
        End Function
    
        ' P/Invoke declarations
        <DllImport("user32.dll")> _
        Private Shared Function WindowFromPoint(pt As Point) As IntPtr
        End Function
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
        End Function
    Then, the mouse wheel message is automatically sent to any control that is currently under the mouse pointer.

    Thanks

  12. #52

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,382

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hi siglr, welcome to VB.Forums. Thanks for your suggestion of using a MessageFilter. I tried your code and it seems to work well.

    I wonder if we really need to find the window to send the filtered message to? After all, the only window we are concerned with is that of the ZoomPictureBox instance itself. Then we could skip the WindowFromPoint function, and slim the PreFilterMessage function down to this:
    Code:
    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
    
    If m.Msg = &H20A Then SendMessage(Me.Handle, m.Msg, m.WParam, m.LParam) Return True Else Return False End If
    End function
    It does appear to work as intended on a quick test.

    However, I don't see why putting Me.Select in the OnMouseEnter should be considered less elegant. The effect is the same and it's certainly a lot shorter. Maybe you can convince me why I am wrong! I have to admit, on looking again at the code for the ZoomPictureBox, I overrode OnMove and OnSizeChanged with Me.Select, and put it in OnMouseDown too. I can't remember why I did that. Probably it was just some debugging effort I forgot to erase. Now that's inelegant!

    BB

  13. #53
    New Member
    Join Date
    Sep 2013
    Posts
    2

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Well, for our data-entry software, we don't want the focus moving out of the text input fields while the user works with the image (zoom or drag), so that's why it's important for the control not to grab the focus.

    Also, the reason I need to find the control to send the message to, is that I have two controls that need to support mouse wheel events. So I need to send it to the right one (the one below the mouse pointer).

    Thanks!

  14. #54

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,382

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Thanks for your explanation. I see your point about the TextBoxes. However, I don't think the MessageFilter code belongs in the ZoomPictureBox definition, because it affects the behaviour of other controls in the same application. Besides, if an application has multiple ZoomPictureBoxes, it would be inefficient to install a new filter with each instance. I guess it would be best to put the filter code on the startup Form (in a simple application) or, better, in a separate class.

    I admit my first point applies just as much to using Me.Select in the OnMouseEnter sub: it affects other controls, so it really shouldn't be there. But it's a simple fix which I think improves the ergonomics of the control. If I ever get round to a new version, I'll add a comment that it's a bit of a hack and should ideally be replaced by a proper message filter, with reference to your posts.

    Thanks again,
    BB

  15. #55
    New Member
    Join Date
    Jul 2014
    Posts
    1

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    how can use this functionality to c#. Thank you

  16. #56

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    2,382

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    The ZoomPictureBox should work equally well in any Windows Form application, regardless of your programming language.

    So you could build the ZoomPictureBox Class using any Visual Studio version or VB.Net Express edition from 2005+. Then add the dll file to your ToolBox as explained in Post #1.

    Alternatively, you could run the code through a VB.Net to Csharp converter first. If necessary Google for an online or downloadable converter. Then add the class to your own project, or build it separately and add the dll to it the Toolbox as above.

    BB

Page 2 of 2 FirstFirst 12

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.