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:
You can add ResizePicture sub in a module (declare it Public) if you want.VB Code:
Private Sub Form_Load() Dim lPic As Picture Me.Picture1.AutoRedraw = True Set lPic = LoadPicture("C:\YourPicture.jpg") 'Use the correct path and filename here ResizePicture Me.Picture1, lPic End Sub Private Sub ResizePicture(pBox As PictureBox, pPic As Picture) Dim lWidth As Single, lHeight As Single Dim lnewWidth As Single, lnewHeight As Single 'Clear the Picture in the PictureBox pBox.Picture = Nothing 'Clear the Image in the Picturebox pBox.Cls 'Get the size of the Image, but in the same Scale than the scale used by the PictureBox lWidth = pBox.ScaleX(pPic.Width, vbHimetric, pBox.ScaleMode) lHeight = pBox.ScaleY(pPic.Height, vbHimetric, pBox.ScaleMode) 'If image Width > pictureBox Width, resize Width If lWidth > pBox.ScaleWidth Then lnewWidth = pBox.ScaleWidth 'new Width = PB width lHeight = lHeight * (lnewWidth / lWidth) 'Risize Height keeping proportions Else lnewWidth = lWidth 'If not, keep the original Width value End If 'If the image Height > The pictureBox Height, resize Height If lHeight > pBox.ScaleHeight Then lnewHeight = pBox.ScaleHeight 'new Height = PB Height lnewWidth = lnewWidth * (lnewHeight / lHeight) 'Risize Width keeping proportions Else lnewHeight = lHeight 'If not, use the same value End If 'add resized and centered to Picturebox pBox.PaintPicture pPic, (pBox.ScaleWidth - lnewWidth) / 2, _ (pBox.ScaleHeight - lnewHeight) / 2, _ lnewWidth, lnewHeight 'Update the Picture with the new image if you need it Set pBox.Picture = pBox.Image End Sub
The VB.NET version is here.




Reply With Quote