Results 1 to 4 of 4

Thread: Run Time Image Resizing

Threaded View

  1. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Run Time Image Resizing

    If you want to add a picture in a Picturebox or in the form using the control's size, you can do it using PaintPicture.

    Using a PictureBox, you can do it like this:
    VB Code:
    1. Private Sub Form_Load()
    2. Dim lPic As Picture
    3.  
    4.     Me.Picture1.AutoRedraw = True
    5.     Set lPic = LoadPicture("C:\YourPicture.jpg") 'Use the correct path and filename here
    6.     ResizePicture Me.Picture1, lPic
    7. End Sub
    8.  
    9. Private Sub ResizePicture(pBox As PictureBox, pPic As Picture)
    10. Dim lWidth      As Single, lHeight    As Single
    11. Dim lnewWidth   As Single, lnewHeight As Single
    12.  
    13.     'Clear the Picture in the PictureBox
    14.     pBox.Picture = Nothing
    15.    
    16.     'Clear the Image  in the Picturebox
    17.     pBox.Cls
    18.    
    19.     'Get the size of the Image, but in the same Scale than the scale used by the PictureBox
    20.     lWidth = pBox.ScaleX(pPic.Width, vbHimetric, pBox.ScaleMode)
    21.     lHeight = pBox.ScaleY(pPic.Height, vbHimetric, pBox.ScaleMode)
    22.    
    23.     'If image Width > pictureBox Width, resize Width
    24.     If lWidth > pBox.ScaleWidth Then
    25.         lnewWidth = pBox.ScaleWidth              'new Width = PB width
    26.         lHeight = lHeight * (lnewWidth / lWidth) 'Risize Height keeping proportions
    27.     Else
    28.         lnewWidth = lWidth                       'If not, keep the original Width value
    29.     End If
    30.    
    31.     'If the image Height > The pictureBox Height, resize Height
    32.     If lHeight > pBox.ScaleHeight Then
    33.         lnewHeight = pBox.ScaleHeight                   'new Height = PB Height
    34.         lnewWidth = lnewWidth * (lnewHeight / lHeight)  'Risize Width keeping proportions
    35.     Else
    36.         lnewHeight = lHeight                            'If not, use the same value
    37.     End If
    38.    
    39.     'add resized and centered to Picturebox
    40.     pBox.PaintPicture pPic, (pBox.ScaleWidth - lnewWidth) / 2, _
    41.                             (pBox.ScaleHeight - lnewHeight) / 2, _
    42.                             lnewWidth, lnewHeight
    43.                            
    44.     'Update the Picture with the new image if you need it
    45.     Set pBox.Picture = pBox.Image
    46. End Sub
    You can add ResizePicture sub in a module (declare it Public) if you want.
    The VB.NET version is here.
    Last edited by jcis; Jan 13th, 2007 at 09:40 PM.

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