Resize and Center Image Frame (1 Viewer)

Status
Not open for further replies.

ssteinke

for what it's worth
Local time
Today, 10:41
Joined
Aug 2, 2003
Messages
195
This example shows a way to Resize and Center the image frame to fit the image regardless of the image size. Images larger than the frame will be reduced to fit the frame. Images smaller than the frame will remain their normal size. Simply call the function named fResizeImageFrame().

This also provides a way to apply a border to your pictures at runtime.

You must link to a picture in the image frame in order to use this example.

Scott

EDIT: See below for corrected code
 

Attachments

  • Resize Image Frame.zip
    43 KB · Views: 3,030
Last edited:

ssteinke

for what it's worth
Local time
Today, 10:41
Joined
Aug 2, 2003
Messages
195
here ya go

EDIT: See below for corrected code
 

Attachments

  • Resize Image Frame 2000.zip
    40.6 KB · Views: 1,399
Last edited:

ssteinke

for what it's worth
Local time
Today, 10:41
Joined
Aug 2, 2003
Messages
195
The posted sample works fine for a perfectly square image frame, however, it was pointed out that when a frame is wider than an image that is also wide, the function causes the picture to be adjusted outside the boundary of the frame instead of the frame adjusting to the picture.

The following code fixes the issue by calculating a dimensional percentage of the frame and picture and then comparing the two, essentially giving the desired effect of the frame adjusting to the picture, regardless of the size of either.

THIS IS THE CORRECTED CODE:

Code:
[COLOR="DarkGreen"]'This function will resize and center an image frame based on the
'dimensions of any image.[/COLOR]
Public Function fResizeImageFrame(ctl As Control)
On Error GoTo Err_fResizeImageFrame

    Dim fraLeft As Integer
    Dim fraTop As Integer
    Dim fraHgt As Integer
    Dim fraWdt As Integer
    Dim picHgt As Integer
    Dim picWdt As Integer
    Dim pct As Double
    Dim fra As Double
    Dim pic As Double
    
    [COLOR="DarkGreen"]'get dimensions of the image frame[/COLOR]
    fraLeft = ctl.Left
    fraTop = ctl.Top
    fraHgt = ctl.Height
    fraWdt = ctl.Width
    
    [COLOR="darkgreen"]'get dimensions of the image in the frame[/COLOR]
    picHgt = ctl.ImageHeight
    picWdt = ctl.ImageWidth

    [COLOR="darkgreen"]'get a percent value for the frame and image
    'which is based on the dimensions of each[/COLOR]
    fra = fraWdt / fraHgt
    pic = picWdt / picHgt

[COLOR="darkgreen"]'pics dimensions smaller than entire frame[/COLOR]
    If fraHgt > picHgt And fraWdt > picWdt Then
        [COLOR="darkgreen"]'resize frame to fit pic[/COLOR]
        ctl.Height = picHgt
        ctl.Width = picWdt
        [COLOR="darkgreen"]'center frame[/COLOR]
        ctl.Left = fraLeft + ((fraWdt - picWdt) / 2)
        ctl.Top = fraTop + ((fraHgt - picHgt) / 2)
[COLOR="darkgreen"]'pics dimensions taller than frame dimensions[/COLOR]
    ElseIf pic < fra Then
        [COLOR="darkgreen"]'determine percentage the pic is being reduced[/COLOR]
        pct = fraHgt / picHgt
        [COLOR="darkgreen"]'calculate the new pic width[/COLOR]
        picWdt = picWdt * pct
        [COLOR="darkgreen"]'resize frame to fit pic[/COLOR]
        ctl.Width = fraWdt - (fraWdt - picWdt)
        [COLOR="darkgreen"]'center frame[/COLOR]
        ctl.Left = fraLeft + ((fraWdt - picWdt) / 2)
[COLOR="darkgreen"]'pics dimensions wider than frame dimensions[/COLOR]
    ElseIf pic > fra Then
        [COLOR="darkgreen"]'determine percentage the pic is being reduced[/COLOR]
        pct = fraWdt / picWdt
        [COLOR="darkgreen"]'calculate the new pic height[/COLOR]
        picHgt = picHgt * pct
        [COLOR="darkgreen"]'resize frame to fit pic[/COLOR]
        ctl.Height = fraHgt - (fraHgt - picHgt)
        [COLOR="darkgreen"]'center frame[/COLOR]
        ctl.Top = fraTop + ((fraHgt - picHgt) / 2)
    End If

Exit_fResizeImageFrame:
    Exit Function
Err_fResizeImageFrame:
    MsgBox Err.Description, vbCritical, "Error " & Err.Number
    Resume Exit_fResizeImageFrame
End Function

To Call:
Code:
Call fResizeImageFrame(Me.ImageFrameName)
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom