by Ben White (3 Submissions)
Category: Graphics
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating:
(3 Votes)
This is a very simple way to view a thumbnail of an image file while maintaining the aspect ration.
Inputs
'String - The full path to the image file
'PictureBox1 - Temporary holding place for the image
'PictureBox2 - Final resting place for the image
Option Explicit
Public Sub ViewImage(strFile As String, picTemp As PictureBox, picTarget As PictureBox)
Dim x&, y&, x1&, y1&, z1!
Dim sNoPreview$
On Error GoTo ErrorHandler
'Set default stuffs
picTarget.Cls
picTarget.AutoRedraw = True
picTemp.Visible = False
picTemp.AutoSize = True
'get target sizing info
x = picTarget.width
y = picTarget.height
'Load the image
picTemp.Picture = LoadPicture(strFile)
'get source sizing info
x1 = picTemp.width
y1 = picTemp.height
'Determin conversion ratio to use
z1 = IIf(x / x1 * y1 < y, x / x1, y / y1)
'Calculate new image size
x1 = x1 * z1
y1 = y1 * z1
'Draw Image
picTarget.PaintPicture picTemp.Picture, (x - x1) / 2, (y - y1) / 2, x1, y1
Exit Sub
ErrorHandler:
'set temp image to nothing
picTemp.Picture = LoadPicture()
'Display default error message
sNoPreview = "No Preview Available"
picTarget.CurrentX = x / 2 - picTarget.TextWidth(sNoPreview) / 2
picTarget.CurrentY = y / 2 - picTarget.TextHeight(sNoPreview) / 2
picTarget.Print sNoPreview
End Sub