Results 1 to 21 of 21

Thread: Zoom picturebox with static edges (for report)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Zoom picturebox with static edges (for report)

    My report is an image in a picturebox. I want it so if the user clicks on it it will zoom larger --- while the picturebox remains the same size. That's important. The examples I've found for picturebox zooming cause the picturebox to get bigger and smaller. I need it to be like an Access report. Click on the report and it gets bigger. Click again and it goes back to the full image. Of cours when you're zoomed in, the perimeter of the report is not visible. So I'd like to be able to drag the image as well so the unseen portions can be seen. Does anyone know of an example or tutorial in VB.Net for this type of picturebox function?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Zoom picturebox with static edges (for report)

    here's a custom control that zooms the image when you doubleclick it + allows you to drag the image.
    needs a bit more work on the location after resizing:

    vb Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class dragMoveZoomPicturebox
    4.     Inherits Panel
    5.  
    6.     <DllImport("user32.dll")> _
    7.     Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    8.     End Function
    9.  
    10.     <DllImport("user32.dll")> _
    11.    Public Shared Function ReleaseCapture() As Boolean
    12.     End Function
    13.  
    14.     Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    15.     Private Const HTCAPTION As Integer = 2
    16.  
    17.     Dim standard As Size
    18.  
    19.     Private WithEvents pb1 As New PictureBox
    20.  
    21.     Private _image As Image
    22.     Public Property Image() As Image
    23.         Get
    24.             Return _image
    25.         End Get
    26.         Set(ByVal value As Image)
    27.             _image = value
    28.             pb1.Image = _image
    29.         End Set
    30.     End Property
    31.  
    32.     Public Sub New()
    33.         MyBase.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
    34.         MyBase.Controls.Add(pb1)
    35.         pb1.SizeMode = PictureBoxSizeMode.AutoSize
    36.     End Sub
    37.  
    38.     Private Sub pb1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb1.MouseDoubleClick
    39.         If pb1.SizeMode = PictureBoxSizeMode.AutoSize Then
    40.             standard = pb1.Size
    41.             Dim p As Point = pb1.Location
    42.             pb1.SizeMode = PictureBoxSizeMode.StretchImage
    43.             pb1.Size = New Size(CInt(standard.Width * 1.5), CInt(standard.Height * 1.5))
    44.             pb1.Location = New Point(CInt(p.X * 1.5), CInt(p.Y * 1.5))
    45.         Else
    46.             pb1.SizeMode = PictureBoxSizeMode.AutoSize
    47.             pb1.Size = standard
    48.         End If
    49.     End Sub
    50.  
    51.     Private Sub pb1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pb1.MouseMove
    52.         If e.Button = Windows.Forms.MouseButtons.Left Then
    53.             ReleaseCapture()
    54.             SendMessage(pb1.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
    55.         End If
    56.     End Sub
    57.  
    58. End Class

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    I'm looking for code that will work with a picturebox.

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    Put the PictureBox inside a Panel and set the Panel's AutoScroll property to True. Then use one of the methods you already have for zooming the picture box complete with the image. Only the part of the PictureBox which is inside the panel will be visible. The panel will show scroll bars when necessary to allow the image to be scrolled.

    BB

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Zoom picturebox with static edges (for report)

    Quote Originally Posted by projecttoday View Post
    I'm looking for code that will work with a picturebox.
    my custom control is a picturebox inside a panel

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    I tried putting the picture in a panel and made the changes in the program but there's a problem. When the picturebox is clicked the previous image on the upsized picturebox is upsized correctly but it is cut off on the right and on the bottom.

    Now the picturebox is inside a panel. On clicking the picturebox, the height and width of the picturebox are doubled, causing scroll bars to appear, and the contents of the picture box are regenerated with an adjusted font size and adjusted coordinates. The part that does appear is correct. But it is cut off on the right and on the bottom.

    This is the code that prints one of the lines which is cutoff on the right:

    Code:
                Try
                    gr.DrawString(text, New Font(printFont, printFontSize, printfontstyle, GraphicsUnit.Pixel), printbrush, xadjusted, yadjusted)
                Finally
                    gr.Dispose()
                End Try
    The field text contains a string of characters. In the upsized picturebox the text from text gets cut off on the right. It's cutoff about 4/5 of the way over. The area that's okay is still larger that the original picturebox. That's what's so strange about it. The original report prior to clicking the picturebox appears okay. Any idea why it's doing this? Unless I'm wrong, the DrawString prints the given text at the given coordinate. There is no end point specified. Why should it just quit at some point?
    Last edited by projecttoday; Jul 10th, 2011 at 02:06 AM.

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    @ProjectToday. (EDIT) What happens when you leave out the Try-End Try? It may be masking some coding error. Apart from that, it will be hard to help without seeing more code and maybe a screenshot.

    @.Paul. Of course, the method I mentioned is similar in broad respects to your class: a PictureBox in a Panel. The main difference is that I suggest using the Panel scroll bars instead of Interop to drag the picture box. It may not work as smoothly in some circumstances but it is simpler.



    BB

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    I took a look at .Paul.'s post but was concerned about trying something which he says "needs a bit more work".

    Unfortunately when I took out Try, End, Finally the result was the same. This is the subroutine which prints the line which is cut off on the right

    Code:
        Private Sub Println(ByVal text As String, ByVal x As Single, ByVal y As Single, _
            ByVal printFont As String, ByVal printfontstyle As FontStyle, _
            ByVal fontsize As Single, ByVal brushcolor As Color, _
            ByVal printstatus As String, ByVal gr As Graphics, ByVal ev As PrintPageEventArgs)
            Dim printbrush As Brush      
            printbrush = New SolidBrush(brushcolor)
    
            Dim printFontSize As Double, xadjusted As Single, yadjusted As Single
            If printstatus = "preview" Then
                ' the parameters must be adjusted to fit the size of the output
                printFontSize = fontsize * dRatio         'scale font size for picture box
                xadjusted = x * dRatio
                yadjusted = y * dRatio
                'Try
                gr.DrawString(text, New Font(printFont, printFontSize, printfontstyle, GraphicsUnit.Pixel), printbrush, xadjusted, yadjusted)
                'Catch exc As Exception
                'MsgBox(exc.Message)
                'Finally
                'gr.Dispose()
                'End Try
            ElseIf printstatus = "print" Then
                Dim prntfont As Font = New System.Drawing.Font(printFont, fontsize, _
                    System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                ev.Graphics.DrawString(text, prntfont, printbrush, x, y)
            End If
            printbrush.Dispose()
        End Sub
    This is the line which calls it

    Code:
                Println(str, 2.84 * pointsperinch, 0.6 * pointsperinch, "Eras Bold ITC", _
                    FontStyle.Regular, 18, namecolor, printstatus, Graphics.FromImage(bmp), ev)
    str contains "abcdefghijklmnopqrstuvwxyz1234567890" and this appears in the picturebox before the upsizing. After the upsizing only abcdefghijklmnopqrstuvwxyz12" appears even though there's plenty of room for the rest of it and the full string makes it into the subroutine (I checked). The entire rest of the document is cut off at that same point and it is also cut off at the bottom.

    2.84 is the number of inches from the left the line appears (on the actual paper) and 0.8 is the number of inches from the top the line appears.

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    Are you sure the bitmap is getting resized correctly to match the PictureBox when you zoom it? You could check that quickly by giving the bitmap a contrasting background colour. For example, you could add this line for testing purposes (before gr.DrawString):
    Code:
    gr.Clear(Color.Red)
    I have to come back on my suggestion of using the Panel scrollbars to scroll the PictureBox. I haven't been able to get it to work really well -- the scrollbars themselves cause lots of jerking and flashing. So it would be better to drag the PictureBox with the mouse, and that comes down to practically the same as .Paul's method (although there are other ways of dragging). I suspect a "bit more work" refers the adjustments needed to keep the Image centred reliably during zooming.

    In fact I already have made a custom image zoom/pan control. Everything works perfectly, including zooming to the cursor position. At present it's overcomplicated (too many options) but I think I could boil it down to essentials. Zooming and dragging are done by changing the size and position of a rectangle, and the image is painted into that rectangle. Let me know if you are interested.

    BB

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Zoom picturebox with static edges (for report)

    Quote Originally Posted by boops boops View Post
    I suspect a "bit more work" refers the adjustments needed to keep the Image centred reliably during zooming.
    yep. spot on

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    The red does the same thing. It's cut off on the side and the bottom.

    This picturebox was actually downsized once earlier. The original was a larger form. I decided to reduce the size of the form and the picture box and because the code multiplies everything by a scaling factor it worked. But the image is too small to see detail. So I decided to add a click to upsize it. The current viewable area of the upsized image looks suspiciously like the size of the old/original picturebox.

    In any case it must be a setting somewhere but I can't find it. I am not at all expert in VB.Net drawing code.

    The scroll bars are working fine.

    Also, the hard copy print works fine.
    Last edited by projecttoday; Jul 10th, 2011 at 02:54 PM.

  12. #12
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    Quote Originally Posted by projecttoday View Post
    The red does the same thing. It's cut off on the side and the bottom.

    This picturebox was actually downsized once earlier. The original was a larger form. I decided to reduce the size of the form and the picture box and because the code multiplies everything by a scaling factor it worked. But the image is too small to see detail. So I decided to add a click to upsize it. The current viewable area of the upsized image looks suspiciously like the size of the old/original picturebox.

    In any case it must be a setting somewhere but I can't find it. I am not at all expert in VB.Net drawing code.

    The scroll bars are working fine.

    Also, the hard copy print works fine.
    Well I'm sorry, I can't find it either. I wonder why? BB

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    Thanks for trying.

    Anybody?

  14. #14
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    I'm sorry if I was sarcastic. I have to guess that you are using the bitmap as the PictureBox.Image. It's clear that your bitmap is smaller than you expect. You haven't said how you are resizing the PictureBox or how you are setting the size of the bitmap. How can anyone debug that if you don't show the relevant code?

    The normal (simple) approach is to set the PictureBox.SizeMode to AutoSize and zoom by changing the size of its Image. Have you tried it that way?

    BB

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    Autosize didn't work. I'm doing my own zooming and it works. It's just cut off for some reason. This is the code

    Code:
            Picturebox1.Height =* 2
           standardratio = 8.5 / 11.0
            pictureboxheight = PictureBox1.Height
            PictureBox1.Width = pictureboxheight * standardratio
            pointsperinch = 98
            'scale the report on the screen to an 8.5 by 11 sheet of paper
            dRatio = (pictureboxheight / pointsperinch) / 11.0

  16. #16

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    1. Everywhere. Everything, including the fonts, gets multiplied by dRatio.
    2. Normal

  18. #18
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    Quote Originally Posted by projecttoday View Post
    1. Everywhere. Everything, including the fonts, gets multiplied by dRatio.
    Everywhere? That's tough, because it's where you need to look to find what you are doing wrong. My psychic debugging powers have run out for today. If you want someone to help you, I suggest you take the time to figure out what code shows exactly what you are doing and post it. BB

  19. #19
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Zoom picturebox with static edges (for report)

    You may find it interesting to try out the ZoomPictureBox control I have just posted to the VB.Net code bank. The approach is clearly different from what you are doing but maybe it would serve your purposes. BB

  20. #20
    Member
    Join Date
    Nov 2003
    Posts
    62

    Re: Zoom picturebox with static edges (for report)

    I made this in VB6 that sounds like what you want to do... maybe you can convert it to .net?
    Attached Files Attached Files

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Zoom picturebox with static edges (for report)

    Thanks for your trouble but my form is basically working except for some reason the drawing is clipped off. When I get a chance I'm going to recreate the whole thing, checking it along the way, and that will probably solve the problem. In the meantime I'm using a zoom factor of 1.5 instead of 2.

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