Page 2 of 3 FirstFirst 123 LastLast
Results 41 to 80 of 91

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
    3,201

    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
    3,201

    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
    3,201

    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
    3,201

    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
    3,201

    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
    3,201

    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

  17. #57
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hi BB,
    Thanks for the excellent zoom pic box control. I have applied it to a problem but need some further advice.

    I would like to click on the image in the ZPB and have it return the "image" x,y pixel coordinates, regardless of the zoomfactor or pan position.

    Is there a way that the ZPB control could return those values ? The aim is to zoom the image in on a feature and then click the mouse on the feature to get the position.

    Any help would be greatly appreciated.

  18. #58

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Here's a function you can use to get the click point in image coordinates:
    Code:
    	Private Function ImagePoint(zpb As ZoomPictureBox, mousePoint As Point) As Point
    		With zpb
    			Dim dx As Integer = mousePoint.X - .ImagePosition.X
    			Dim dy As Integer = mousePoint.Y - .ImagePosition.Y
    			Dim zx As Integer = CInt(dx / .ZoomFactor)
    			Dim zy As Integer = CInt(dy / .ZoomFactor)
    			Return New Point(zx, zy)
    		End With
    	End Function
    
    	'example of use of ImagePoint function:
    	Private Sub ZoomPictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles ZoomPictureBox1.MouseDown
    		Console.WriteLine(ImagePoint(ZoomPictureBox1, e.Location).ToString)
    	End Sub
    I'll make this a property of the ZoomPictureBox itself, whenever I get around to a new version.

  19. #59
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Thanks BB, that is exactly what I was after !


  20. #60
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hi BB,
    One other question, if I want to draw a graphic on the image, say a small circle at the click point on the image, how would you suggest this is done ? I would like the graphic to move with the image zoom/pan. Write to the pixel array or an overlay approach ?

  21. #61

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hi Ryan, sorry I neglected your question. It's been a busy time for me.

    I think should be efficient enough to paint the superimposed image with Graphics.DrawImage (or draw the circle with Graphics.DrawEllilpse). See the SuperimposeImage function in the demo project for an example. I don't know whether using a pixel array to do the superimposition would have any advantage. My guess is that the difference probably won't be very much, and it may go one way or the other depending on the sizes of the images.

    BB

  22. #62
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    line 82-83 in your demo

    If sfd.FileName.ToUpper.EndsWith("PNG") Then
    _BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Png)


    gives an error when I tried to save result image from PNG file:
    Object reference not set to an instance of an object.

  23. #63

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Quote Originally Posted by Goshx View Post
    line 82-83 in your demo

    If sfd.FileName.ToUpper.EndsWith("PNG") Then
    _BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Png)

    gives an error when I tried to save result image from PNG file:
    Object reference not set to an instance of an object.
    The base image exists if you load one. Alternatively you can stamp the zoomable image on a blank background. Otherwise there isn't anything to save.

    The image composer is just a quick demo, not a full-fledged program. If you think it's necessary to check whether there is anything to save before you try to save it, add it to your own code.

    BB

  24. #64
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Well... Playing with ImagePosition and ZoomFactor properties I've mentioned that they won't restore the same position as it was before. As higher zooming is as larger error we get.

  25. #65

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Quote Originally Posted by Goshx View Post
    Well... Playing with ImagePosition and ZoomFactor properties I've mentioned that they won't restore the same position as it was before. As higher zooming is as larger error we get.
    Yes, the position does tend to drift slightly when you zoom in and out heavily. The formulas in the code are inevitably a bit approximate (for example because the mouse position doesn't map to an exact image pixel when the zoom factor is less than 1) but there may be something making matters worse than necessary. I hope to find some time to look into it soon.

    Meanwhile, I've spotted a rather stupid error in the ZoomPictureBox_Public Properties.vb file. The Set clause of the EnableMouseWheelZooming property should have
    _EnableMouseWheelZooming = Value
    instead of =True. That won't affect your problem, but it could be annoying for anyone trying to disable zooming with the mouse wheel.

    BB

  26. #66
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    I wouldn't say it is slightly. First time when that happened I suspected that I made some mistake into code because window was completely blank. After that I started to move cursor and mouse wheel and... my image (4000,3000px) became visible as a small thumbnail. Far away behind window's borders.

    Also, when you manipulate with picturebox content without moving of mouse whell and without dragging, image continues to change its starting positions in equal offsets.

  27. #67
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Goshx,
    You did put in a fix for one known issue where the zoom was being applied as a delta to the current size, rather than as a current zoom value to the original size didn't you.
    That was mentioned in the other thread that you started, and I'll quote it here. There may be other things like that.

    Quote:
    I noticed while testing that sometimes when I was zoomed in and drawing near the bottom of the image, that the drawing was offset quite aways from the mouse (always in Y in my case with several different images, but don't know if that would always be the case).

    I determined the reason for this is that the bounds (_ImageBounds.Width and Height) ratios had drifted.
    The Width and Height values were initially different in my images, but if you zoomed way out, then zoomed way in, because of the calculation of the width and height always being in reference to themselves (and not the original image) and limited to an Int (CInt), each would round to an Int at different points and eventually the Width and Height would become square to be in phase with each other.

    So, I just modified those two lines where the Width and Height of the bounds was calculated to fix that by referencing the original size of the image. The other places where you use zoomRatio for recentering are fine as I assume once the bounds are correct, other calculations will take care of themselves.
    Code:
    'In ZoomPictureBox_Main.vb
        'Calculate the image bounds for a given ZoomFactor,  
        Private Function GetZoomedBounds() As Rectangle
    
            'Find the zoom center relative to the image bounds.
            Dim imageCenter As Point = FindZoomCenter(_ZoomMode)
    
            'Calculate the new size of the the image bounds.
            _previousZoomfactor = _ImageBounds.Width / _Image.Width
            If Math.Abs(_ZoomFactor - _previousZoomfactor) > 0.001 Then
                Dim zoomRatio As Double = _ZoomFactor / _previousZoomfactor
          '   _ImageBounds.Width = CInt(_ImageBounds.Width * zoomRatio)  'Fixed these two lines so they don't accumulate Integer rounding values
          '   _ImageBounds.Height = CInt(_ImageBounds.Height * zoomRatio)
                _ImageBounds.Width = CInt(_Image.Width * _ZoomFactor)    
                _ImageBounds.Height = CInt(_Image.Height * _ZoomFactor)
    
                'Find the resulting position of the zoom center prior to correction.
                Dim newPRelative As Point
                newPRelative.X = CInt(imageCenter.X * zoomRatio)
                newPRelative.Y = CInt(imageCenter.Y * zoomRatio)
    
                'Apply a correction to return the zoom center to its previous position.
                _ImageBounds.X += imageCenter.X - newPRelative.X
                _ImageBounds.Y += imageCenter.Y - newPRelative.Y
    
            End If
            _previousZoomfactor = _ZoomFactor
            Return _ImageBounds
        End Function

  28. #68
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Yes, I've seen your post but didn't know that it was related with my question at that moment.

    When I changed code to your, things are a bit better now (especially when image is not large and when it is centered). Unfortunately, problem still persists. Large zoomed images makes huge difference between original position and new one calculated from ImagePosition and ZoomFactor.

  29. #69

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Thanks to you both, Goshx and Passel, for identifying some problems with the ZoomPictureBox. There seem to be two different issues.

    1. There is indeed something wrong with the "save view / restore view" logic. If only the zoom, or only the position, changes after saving the view, the restore works correctly. But after both zoom and position have changed, the restore goes wrong. That means I need to debug what happens in and after the ZoomFactor and ImagePosition property Set clauses.

    2. A certain amount of imprecision happens due to the large zoom range. When zoomed right out (as an extreme example, a 2000 pixel wide image displayed at 10 pixels wide i.e. ZoomFactor 0.05) it's not surprising that it's difficult to use the mouse precisely. A microbe passing wind near your mouse could cause a jump of 50 image pixels, so to speak. When you zoom right in (magnification > * 4 for example) the "pixels" tend to jiggle about due to the way GDI+ renders highly magnified images. But I don't see why this should result in a persistent shift. The corrections in the post above, although "cleaner code", above don't seem to make any visible difference here. I wonder if it's just a matter of rounding errors? I'll try replacing rectangles and points by RectangleFs and PointFs wherever possible, to see if it helps.

    BB

  30. #70
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    an experienced VS programmer would find "the heck" without many problems, I think

  31. #71

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Quote Originally Posted by Goshx View Post
    an experienced VS programmer would find "the heck" without many problems, I think
    Well I don't even know what "the heck" is let alone how to find it! Never mind, I think I've got a handle on both problems.

    re: 1
    As long as you restore first the Zoom then the ImagePosition, save/restore seems to work fine. That's no doubt because setting ZoomFactor changes the ImagePosition, but not vice versa. I'll have to think how to build that into the control in some idiot-proof way.

    re: 2
    It is indeed a matter of Integer/Single rounding errors. I've replaced all the appropriate Rectangles, Points and CInts by RectangleFs, PointFs and CSngs. Now everything seems to zoom smoothly, or at least drift by no more than 1 pixel per full-range zoom. I must assume that anything more that that is either microbial flatulence or user tremor.

    For those interested, there's a zip of my present (interim) version of the ZPB and TestForm attached. But I'll post a proper update soon.

    BB

  32. #72
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    It is really funny and almost unbelievable that changing order of ImagePosition and ZoomFactor solves the problem. I could bet on 1 million that it is impossible

    That means, you have to isolate/keep ZoomFactor value before calculating of ImagePosition. In any case, this problem has been solved. Thank you.
    Last edited by Goshx; Jan 16th, 2016 at 08:49 AM.

  33. #73

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    I'm glad it works for you too. You can never be sure.

    I'm thinking of dealing with it by adding public SaveView and RestoreView methods to the next version. An alternative I first had in mind was to expose the ImageBounds property as Read/Write instead of ReadOnly. The problem with that is what to do if the user specifies a rectangle with a different aspect ratio to the image. I don't want to throw exceptions from the control if I can avoid it. Something that occurred to me was to stretch the image in one direction only to fit the specified rectangle. But it isn't the job of the ZoomPictureBox to do things like that: it's an image viewing control, not an image editor.

    BB

  34. #74
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: ZoomPictureBox: mod to constrain image to viewable area

    I took your excellent ZoomPictureBox control and made a slight change that you may want to include. I added a "Constrain" property to enable/disable dragging of the image outside of the displayable area. Your control saved me a (rude word)-ton of work. Thanks.

    <Category("_ZoomPictureBox"),
    Description("Constrain image to within visible area")>
    Public Property Constrain As Boolean
    Get
    Return _Constrain
    End Get
    Set(value As Boolean)
    _Constrain = value
    End Set
    End Property

    Private _Constrain As Boolean

    Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)

    If _dragging Then

    Me.Invalidate(_ImageBounds)

    'calculate new image position

    Dim newx As Integer = _ImageBounds.X + e.X - _startDrag.X
    Dim newy As Integer = _ImageBounds.Y + e.Y - _startDrag.Y

    'adjust if Constrain=True to keep as much ofg image as possible in view

    If _Constrain Then

    Select Case True

    'Image will fit in view - shift left or right to avoid clipping
    Case _ImageBounds.Width <= Me.Width
    If newx < 0 Then newx = 0
    If newx + _ImageBounds.Width > Me.Width Then newx = Me.Width - _ImageBounds.Width

    'shift left to remove vertical null space
    Case newx > 0
    newx = 0

    'shift right to remove vertical null space
    Case newx + _ImageBounds.Width < Me.Width
    newx = Me.Width - _ImageBounds.Width

    End Select

    Select Case True

    'Image will fit in view - shift up or down to avoid clipping
    Case _ImageBounds.Height <= Me.Height
    If newy < 0 Then newy = 0
    If newy + _ImageBounds.Height > Me.Height Then newy = Me.Height - _ImageBounds.Height

    'shift up to remove horizontal null space
    Case newy > 0
    newy = 0

    'shift down to remove horizontal null space
    Case newy + _ImageBounds.Height < Me.Height
    newy = Me.Height - _ImageBounds.Height

    End Select

    End If

    _ImageBounds.X = newx
    _ImageBounds.Y = newy
    _startDrag = e.Location

    Me.Invalidate(_ImageBounds)

    End If

    MyBase.OnMouseMove(e)

    End Sub
    Last edited by Reverend Jim; Sep 3rd, 2018 at 05:15 PM. Reason: minor software change

  35. #75

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Hi Rev. Jim, my apologies for not noticing your suggestion before. Yikes, it's been nearly 6 months! Somehow I got unsubscribed from this thread so I didn't get notified.

    Now I have made some improvements to the ZoomPictureBox. Among other things it includes a way to prevent the image getting dragged out of view. The method is much the same as Rev. Jim's, but it deals with more situations (dragging, zooming and control size changed). There is a new DragMargin property that sets the minimum width that always stays visible.

    The main change deals with some performance problems that affected the previous version. With large images (say more than 8 MPixels, depending on hardware), image dragging was jerky and slow when zoomed-in, and zooming could be very slow when zoomed-out. The new version works smoothly with much larger images, for example 30 MPixels. It even works reasonably well for a 200 MPixel image although I don't intend to try it with anything bigger!

    Here's how it works. When you set the Image property, it generates a List of images each reduced by one quarter of the preceding pixel size until a minimum width or height of 128 pixels is reached. The OnPaint sub selects a suitable low-resolution image from the List and paints it immediately; this is much quicker than painting the full-res image. At the same time a System.Timers.Timer is triggered. Once the timer interval elapses, the image is replaced by the full-resolution version.

    The attachments below are a zip file with the revised ZoomPictureBox code (two files, ZoomPictureBox_Main.vb and ZoomPictureBox_Public Properties.vb), and another zip file with code for a test form (TestForm.vb). There's no need to add a ZoomPictureBox to the test form in the designer, it's added in code. The form code allows you to double-click the ZoomPictureBox at runtime to select the image.

    BB

  36. #76
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    No problem. Thanks for the update. I can't wait to check it out.

  37. #77
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    I'm having several problems with the new code under Visual Studio 2017.

    I load up the previous project from ZoomPictureBox+TestForm+Demo.zip and let VS2017 do the conversion.

    I run the previous code after the rebuild to make sure it works (it does).

    I replace the previous versions of ZoomPictureBox_Main.vb, ZoomPictureBox_Public Properties.vb and TestForm.vb with their newer version.

    I try a rebuild and get a pile of errors

    Name:  2019-03-02_112606.jpg
Views: 1529
Size:  33.9 KB

    Name:  2019-03-02_112621.jpg
Views: 1518
Size:  35.8 KB

    Could you please zip the current version as a complete VS project and post it?

  38. #78
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    I'm having several problems with the new code under Visual Studio 2017.

    I load up the previous project from ZoomPictureBox+TestForm+Demo.zip and let VS2017 do the conversion.

    I run the previous code after the rebuild to make sure it works (it does).

    I replace the previous versions of ZoomPictureBox_Main.vb, ZoomPictureBox_Public Properties.vb and TestForm.vb with their newer version.

    I try a rebuild and get a pile of errors

    Name:  2019-03-02_112606.jpg
Views: 1529
Size:  33.9 KB

    Could you please zip the current version as a complete VS project and post it?

  39. #79

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

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    Apologies for the long delay. I didn't include a designer file in the TestForm.vb.zip in post #75, but you can get by without it by copying the code into an existing form.

    The following steps work for me in VS2017:
    1. Download the zip files from post #75 to a convenient folder and unzip them.
    2. In Visual Studio 2017, start a new WinForms project. Leave Form1 and everything else unchanged.
    3. Click Add Existing Item in the VS Project menu and select the three unzipped files (e.g. with Shift-select). Ignore any errors for the moment.
    4. Select the code from TestForm.vb (excluding Class and End Class statements) and Cut it using Ctrl-x. Paste it into the default form (Form1). The errors should disappear.
    5. Run the project.

    The test form code enables you to Double-click the ZoomPictureBox to load an image, and change the form size to resize the ZoomPictureBox.

    It's not hard to zoom and pan very large images (e.g. 50 megapixel) in this version. It even worked on my hardware with a 250 megapixel image, although the zooming was no longer smooth. By the way, I download large images for testing purposes from Wikipedia's Picture of the Day.

    BB

  40. #80
    New Member
    Join Date
    Sep 2019
    Posts
    1

    Re: ZoomPictureBox: picture control with mouse-centred zooming

    First of all, thank you for making this available to us in the development community. Your product has been a tremendous addition to my application.

    Is there a way to remove the image from the picture box when we are finished viewing/zooming it? I have a dilemma where I can't delete the image file that has been "Zoom Pictured" after I finish and close the popup window that has it on it.

    Regular PictureBox controls have an .Image property that can be set to Nothing as well as an .Invalidate() method. I can't seem to find those things in your user control. I need a way to be able to delete the image from within my application if the user wishes to do so. However, if I have run the Zoom Picture Box on an image, I can't do that. I get the runtime error that the object is currently in use.

    Thanks in advance,
    Paul

Page 2 of 3 FirstFirst 123 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
  •  



Click Here to Expand Forum to Full Width