Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: zoom in and zoom out

104| Wed, 05 Dec 2007 00:27:00 GMT| junlo| Comments (5)
how to write the code to zoom in or zoom out the picture? can anybody provide the code on zoom in or out function?

Keywords & Tags: zoom, out, microsoft, visual basic, vb

URL: http://www.programmerbase.com/visual-basic/560736/
 
«« Prev - Next »» 5 helpful answers below.
See if this sample (http://www.vbforums.com/showpost.php?p=2003447&postcount=3) works for you.

rhinobull | Mon, 03 Dec 2007 15:59:00 GMT |

See if this sample (http://www.vbforums.com/showpost.php?p=2003447&postcount=3) works for you.

this sample is zoom in or zoom out the picture box' size.
what i want is zoom in or zoom out the actual picture's size which i loaded but the picture box's size is still maintain.

junlo | Mon, 03 Dec 2007 16:00:00 GMT |

Something similar

Private mPic As Picture
Private mWidth As Single, mHeight As Single

Private Sub Form_Load()
Set mPic = LoadPicture("D:\pic1.JPG") '(Add the Path to your Picture here)
mWidth = Picture1.ScaleX(mPic.Width, vbHimetric, Picture1.ScaleMode)
mHeight = Picture1.ScaleY(mPic.Height, vbHimetric, Picture1.ScaleMode)

Picture1.AutoRedraw = True
Picture1.PaintPicture mPic, 0, 0
End Sub

Private Sub Command1_Click()
ZoomPicture Picture1, 0.9
End Sub
Private Sub Command2_Click()
ZoomPicture Picture1, 1.1
End Sub

Public Sub ZoomPicture(pct As PictureBox, zoom As Double)
pct.Cls
mWidth = mWidth * zoom
mHeight = mHeight * zoom
pct.PaintPicture mPic, 0, 0, mWidth, mHeight
End Sub

jcis | Mon, 03 Dec 2007 16:01:00 GMT |

this sample is zoom in or zoom out the picture box' size...
But that was done intentionally or it will look sort of unfinished otherwise.
If you want to keep the orginal controls size place your main picturebox (without the borders) into another picturebox (with the borders) that can work like container.
You will resize picturebox that displays picture but container will stay unchanged.

rhinobull | Mon, 03 Dec 2007 16:02:00 GMT |

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
You can add ResizePicture sub in a module (declare it Public) if you want.

i use code above,but came out an error "object variable or with block variable not set" and point to the line code(red color)

junlo | Mon, 03 Dec 2007 16:03:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories