Results 1 to 9 of 9

Thread: Expand image with animation

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    52

    Expand image with animation

    The code below animates the resizing effect properly but from the left corner of the image. How can it be done from the center expanding in all directions?

    Code:
    Private Sub Timer1_Timer()
    
    If Image1.Width >= 2916 Then
        Image1.Width = 2916
        Timer1.Enabled = False
        Exit Sub
    End If
    Image1.Width = Image1.Width + 50
    Image1.Height = Image1.Height + 50
    End Sub
    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Expand image with animation

    Thread moved from the 'CodeBank VB6' forum (which is for you to post working code examples, not questions) to the 'VB6 and earlier' forum

  3. #3
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,229

    Re: Expand image with animation

    Code:
    Private Sub Timer1_Timer()
    
    If Image1.Width >= 2916 Then
        Image1.Width = 2916
        Timer1.Enabled = False
        Exit Sub
    End If
    Image1.Top = Image1.Top - 25
    Image1.Left = Image1.Left - 25
    Image1.Width = Image1.Width + 50
    Image1.Height = Image1.Height + 50
    End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    52

    Re: Expand image with animation

    It has to expand in ALL directions from the CENTER of the Picture box

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Expand image with animation

    It does expand in all directions... you just need to set the start position (presumably top and left of 2916/2) and size (0).

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Expand image with animation

    In other words, if Image1 is inside a picturbox, you have to center Image1 in the picturebox at the start.
    Then, as the Image1 is expanded, it is also moved (top, left) to keep the center of Image1 at the same location it started, which will be the center of the picturebox, if you had it centered in the picturebox to start with.

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Expand image with animation

    This example preserves the aspect ratio of the specified image as it is enlarged:

    Code:
    Option Explicit 'Copy & paste in a blank Form
    
    Private Const HALF        As Single = 0.5!
    
    Private m_AspectRatio     As Single
    Private Image1            As VB.Image
    Private Picture1          As VB.PictureBox
    Private WithEvents Timer1 As VB.Timer
    
    Private Sub Form_Activate()
        Const PROMPT = "No picture data or picture filename found in the Clipboard!" & vbCrLf & vbCrLf & "Try again?"
        Const STYLES = vbCritical Or vbRetryCancel Or vbDefaultButton1
    
        WindowState = vbMaximized
    
        Set Picture1 = Controls.Add("VB.PictureBox", "Picture1")
        Set Image1 = Controls.Add("VB.Image", "Image1", Picture1)
    
        Do: Select Case True
                Case Clipboard.GetFormat(vbCFText):   On Error Resume Next
                                                      Image1 = LoadPicture(Replace(Clipboard.GetText, """", vbNullString))
                                                      On Error GoTo 0
    
                Case Clipboard.GetFormat(vbCFBitmap): Image1 = Clipboard.GetData
            End Select
    
            If Image1.Picture.Handle Then Exit Do
            If MsgBox(PROMPT, STYLES) = vbCancel Then Unload Me: Exit Sub
        Loop
    
        Picture1.Move ScaleLeft, ScaleTop, ScaleWidth, ScaleHeight
        Picture1.ScaleMode = vbPixels
        Picture1.Visible = True
    
        Image1.Move Picture1.ScaleWidth * HALF, Picture1.ScaleHeight * HALF, 0!, 0!
        Image1.Stretch = True
        Image1.Visible = True
        m_AspectRatio = Image1.Picture.Width / Image1.Picture.Height
    
        Set Timer1 = Controls.Add("VB.Timer", "Timer1")
        Timer1.Interval = 1&
    End Sub
    
    Private Sub Timer1_Timer()
        Dim NewWidth As Single, NewHeight As Single
    
        With Image1
            If .Width < Picture1.ScaleWidth And .Height < Picture1.ScaleHeight Then
                NewWidth = .Width + 2! 'Pixels
                NewHeight = NewWidth / m_AspectRatio
    
               .Move .Left - 1!, .Top - (NewHeight - .Height) * HALF, NewWidth, NewHeight
            Else
                Timer1 = False
            End If
        End With
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Expand image with animation

    Good point.

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2014
    Posts
    52

    Re: Expand image with animation

    Seems to do what I need! Many thanks

Tags for this Thread

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