PictureBox - How to ZOOM in & out?

Hi
can you help me to find out how to make a zoom in and out in this control.
I know how to it by change the size of it and the SizeMode but i need to do it without change its size.
In the past i work with vb6 and the kodak image control but now i work with .Net and XP so i don't want to use this control.

Comments

  • Why not create your own PictureBox with zoom? It's very easy:

    [code]
    Public Class TransformedPictureBox
    Inherits UserControl

    Public Sub New()
    ' .NET initialaztion stuff

    ' add these styles to get rid of flickering
    SetStyle(ControlStyles.AllPaintInWmPaint, True)
    SetStyle(ControlStyles.UserPaint, True)
    SetStyle(ControlStyles.DoubleBuffer, True)
    End SUb

    ' The Image
    Private _image As Image = Nothing
    Public Property Image As Image
    Get
    Return _image
    End Get
    Set
    _image = Value
    Invalidate()
    End Set
    End Property

    ' The transformation matrix
    Private _transform As Matrix = New Matrix()
    Public Property Transform As Matrix
    Get
    Return _transform
    End Get
    End Property

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
    If Not Image Is Nothing
    ' Set the transform matrix and draw
    e.Graphics.MultiplyTransform(Transform)
    e.Graphics.DrawImage(Image, 0, 0)
    End If
    End Sub
    End Class
    [/code]
    That's it. Now you can use the Transform property to do your zooming (and translation, rotation, shearing). Something like:
    ' Load an image, zoom by 200%
    ' myPictureBox is TransformedPictureBox
    myPictureBox.Image = Image.FromFile("bla.jpg")
    myPictureBox.Transform.Scale(2, 2)
    myPictureBox.Invalidate()

    Note that I wrote all the above from my head, so forgive the syntax errors.


    : Hi
    : can you help me to find out how to make a zoom in and out in this control.
    : I know how to it by change the size of it and the SizeMode but i need to do it without change its size.
    : In the past i work with vb6 and the kodak image control but now i work with .Net and XP so i don't want to use this control.
    :

  • : Why not create your own PictureBox with zoom? It's very easy:
    :
    : [code]
    : Public Class TransformedPictureBox
    : Inherits UserControl
    :
    : Public Sub New()
    : ' .NET initialaztion stuff
    :
    : ' add these styles to get rid of flickering
    : SetStyle(ControlStyles.AllPaintInWmPaint, True)
    : SetStyle(ControlStyles.UserPaint, True)
    : SetStyle(ControlStyles.DoubleBuffer, True)
    : End SUb
    :
    : ' The Image
    : Private _image As Image = Nothing
    : Public Property Image As Image
    : Get
    : Return _image
    : End Get
    : Set
    : _image = Value
    : Invalidate()
    : End Set
    : End Property
    :
    : ' The transformation matrix
    : Private _transform As Matrix = New Matrix()
    : Public Property Transform As Matrix
    : Get
    : Return _transform
    : End Get
    : End Property
    :
    : Protected Overrides Sub OnPaint(e As PaintEventArgs)
    : If Not Image Is Nothing
    : ' Set the transform matrix and draw
    : e.Graphics.MultiplyTransform(Transform)
    : e.Graphics.DrawImage(Image, 0, 0)
    : End If
    : End Sub
    : End Class
    : [/code]
    : That's it. Now you can use the Transform property to do your zooming (and translation, rotation, shearing). Something like:
    : ' Load an image, zoom by 200%
    : ' myPictureBox is TransformedPictureBox
    : myPictureBox.Image = Image.FromFile("bla.jpg")
    : myPictureBox.Transform.Scale(2, 2)
    : myPictureBox.Invalidate()
    :
    : Note that I wrote all the above from my head, so forgive the syntax errors.
    :
    :
    : : Hi
    : : can you help me to find out how to make a zoom in and out in this control.
    : : I know how to it by change the size of it and the SizeMode but i need to do it without change its size.
    : : In the past i work with vb6 and the kodak image control but now i work with .Net and XP so i don't want to use this control.
    : :
    :
    :

    Hi Thanks for the help.
    The code zoom in very good but i need help with 2 more issues
    1. When zoom in there is no scroll bar, how can i show the scroll bar (I set AutoScroll = True but it not work)
    2. How can i zoom out ?


  • You zoom out by calling 'myPictureBox.Transform.Scale(0.5, 0.5)
    In general, scale to 1.0 to get back to original size, anything greater than 1 zooms in, less than 1 zooms out.

    To add scrollbars you need to setup the AutoScrollMinSize everytime you load an image or change the scale factor, add this method:
    [code]
    Public Sub SetupAutoScroll()
    If Not Image Is Nothing
    ' Get the image size
    Dim s As SizeF = SizeF.Empty
    s.Width = CType(96.0 * Image.Width / Image.HorizontalResolution, Float)
    s.Height = CType(96.0 * Image.Height / Image.VerticalResolution, Float)

    ' calculate the current scaled size of image
    PointF() pts = New PointF(1)
    pts(0).X = s.Width
    pts(0).Y = s.Height
    _transform.TransformPoints(pts)

    ' that's your autoscroll size
    AutoScrollMinSize = New Size(CType(pts(0).X + 0.5, Integer), CType(pts(0).Y + 0.5, Integer)
    Else
    ' No image, no scrollbars
    AutoScrollMinSize = Size.Empty
    End If
    End Sub
    [/code]

    Also, you need to change your paint code a bit to take care of the current scroll position
    [code]
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    If Not Image Is Nothing Then
    ' first, scroll then transform
    e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y)
    e.Graphics.MultiplyTransform(Transform)
    e.Graphics.DrawImage(Image, 0, 0)
    End If
    End Sub
    [/code]

    Now, whenever you set a new image or scale in or out, you need to call this method:

    ' Load an image, zoom by 200%
    ' myPictureBox is TransformedPictureBox
    myPictureBox.Image = Image.FromFile("bla.jpg")
    myPictureBox.Transform.Scale(2, 2)
    myPictureBox.SetupAutoScroll()
    myPictureBox.Invalidate()

  • [b][red]This message was edited by lee_s at 2003-7-2 2:31:50[/red][/b][hr]
    : You zoom out by calling 'myPictureBox.Transform.Scale(0.5, 0.5)
    : In general, scale to 1.0 to get back to original size, anything greater than 1 zooms in, less than 1 zooms out.
    :
    : To add scrollbars you need to setup the AutoScrollMinSize everytime you load an image or change the scale factor, add this method:
    : [code]
    : Public Sub SetupAutoScroll()
    : If Not Image Is Nothing
    : ' Get the image size
    : Dim s As SizeF = SizeF.Empty
    : s.Width = CType(96.0 * Image.Width / Image.HorizontalResolution, Float)
    : s.Height = CType(96.0 * Image.Height / Image.VerticalResolution, Float)
    :
    : ' calculate the current scaled size of image
    : PointF() pts = New PointF(1)
    : pts(0).X = s.Width
    : pts(0).Y = s.Height
    : _transform.TransformPoints(pts)
    :
    : ' that's your autoscroll size
    : AutoScrollMinSize = New Size(CType(pts(0).X + 0.5, Integer), CType(pts(0).Y + 0.5, Integer)
    : Else
    : ' No image, no scrollbars
    : AutoScrollMinSize = Size.Empty
    : End If
    : End Sub
    : [/code]
    :
    : Also, you need to change your paint code a bit to take care of the current scroll position
    : [code]
    : Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    : If Not Image Is Nothing Then
    : ' first, scroll then transform
    : e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y)
    : e.Graphics.MultiplyTransform(Transform)
    : e.Graphics.DrawImage(Image, 0, 0)
    : End If
    : End Sub
    : [/code]
    :
    : Now, whenever you set a new image or scale in or out, you need to call this method:
    :
    : ' Load an image, zoom by 200%
    : ' myPictureBox is TransformedPictureBox
    : myPictureBox.Image = Image.FromFile("bla.jpg")
    : myPictureBox.Transform.Scale(2, 2)
    : myPictureBox.SetupAutoScroll()
    : myPictureBox.Invalidate()
    :
    :

    Hey you are great.

    What is:
    PointF() pts = New PointF(1)
    pts(0).X
    pts(0).Y

    do i need to declare these objects?




  • : What is:
    : PointF() pts = New PointF(1)
    : pts(0).X
    : pts(0).Y
    :
    : do i need to declare these objects?
    :

    Oh yes, sorry, got my C# crossed with my VB. It should read

    Dim pts(0) as PointF
    pts(0).X = ..
    pts(0).Y = ..

    Matrix.TransformPoints need an array of points, so even we are only transforming one point, we need to put it in an array...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion