Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fitting the imagebox to picture dimension 1

Status
Not open for further replies.

drimades

IS-IT--Management
Joined
Nov 8, 2004
Messages
221
Location
MK
----------------------------------------

I have an imagebox in my form where I load a picture (from database field). I want to fit the imagebox to the foto (preserving the ratio between length and width) and not just stretch the foto to the imagebox.

How can I do it?

Thanks!

----------------------------------------
 
Assume your picture box is named PictureBox1

To set the picturebox to the size of the image, PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize.

To set the image to the size of the picturebox (while maintaining the width-x-height ratio of the image)
1. In you sub routine get the image Width & Height
2. Set a ratio based on 'image.height > picturebox.height' and/or 'image.width > picturebox.width'
3. Load the resized image to the picturebox
Example:
Code:
Sub LoadImage()
    Dim img As Image            'image
    Dim iIHe, iIWi As Integer   'Height, Width of the image
    Dim lRatio as Single        '

    img = Image.FromFile("[i]C:\MyFile.jpg[/i]")
    iIHe = img.Height
    iIWi = img.Width
    With PictureBox1
         .SizeMode = PictureBoxSizeMode.CenterImage
         If iIHe > .Height Then
             lRatio = (iIHe / iIWi) * 100
             iIHe = .Height
             iIWi -= (iIWi / ratio)
         End If
         If iIWi > .Width Then
             lRatio = (iIWi / iIHe) * 100
             iIWi = .Width
             iIHe -= (iIHe / ratio)
         End If
         .Image = img.GetThumbnailImage(iIWi, iIHe, Nothing, Nothing)
    End With
    img.Dispose()
    img = Nothing
End Sub
eyes_015.gif___1129027102664


Maybe this world is another planet’s Hell.
Aldous Huxley
 
--------------------------------

I'm using:

Code:
Dim TempPict As New StdPicture
Set TempPict = LoadPicture(rs.Fields.Item(12))
Dim wdt, hgt As Integer

wdt = Int(TempPict.Width * 3 / 7)
hgt = Int(TempPict.Height * 3 / 7)

With Form5
    .Image1.Width = wdt
    .Image1.Height = hgt
    .Image1.Picture = TempPict
End With

but it doesn't work! It's VB6.
--------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top