Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: Zoom in picturebox

200| Wed, 30 Apr 2008 18:16:00 GMT| sergi| Comments (2)
Hi people!

I want to show a image in picturebox. But if image is bigger picturebox
I can't see all image. If I use a Image control with strech option, it
deform the image.
I want that my image adjust to picturebox without deform this.

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

URL: http://www.programmerbase.com/visual-basic/560743/
 
«« Prev - Next »» 2 helpful answers below.
"Sergi" <sezmillenium...yahoo.es> wrote in message
news:%230VDoSpPFHA.3880...tk2msftngp13.phx.gbl...
> Hi people!
> I want to show a image in picturebox. But if image is bigger picturebox
> I can't see all image. If I use a Image control with strech option, it
> deform the image.
> I want that my image adjust to picturebox without deform this.


Stretch only deforms the image if you don't size the control while keeping
the aspect ratio. iow, if the picture is 800 x 600 (for example), you can
size the image control to 400 x 300 (for example) and the picture will still
look correct.
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..

kenhalter | Wed, 30 Apr 2008 18:18:00 GMT |

> I want to show a image in picturebox. But if image is bigger
picturebox
> I can't see all image. If I use a Image control with strech option, it
> deform the image.
> I want that my image adjust to picturebox without deform this.


Perhaps the following previous post of mine will be of some help to you.

Rick - MVP

You will need to put your picture into an ImageBox and place that
ImageBox into a PictureBox. Then give the following Subroutine a try. It
properly fits the ImageBox (with its Stretch property set to False) into
its container (in this case, a PictureBox, but it would also work if the
container was a Frame, the Form, or anything else that can serve as a
container). So, for your program, use a PictureBox of whatever size you
need as a container for the ImageBox. (Make sure the ImageBox is
*really* contained in the PictureBox and not simply resting on top of
it; cut it from the Form, click on the PictureBox and paste it back into
the now highlighted PictureBox.)

The first parameter is the name you have given to the ImageBox. The
optional 2nd argument allows you to specify a minimum number of pixel
that the image must be away from the closest edge of its container. As
an example, you could call the following subroutine like this (assuming
you place a CommonDialogBox on your form; otherwise, just specifiy the
path/filename, by whatever means you obtain it, as the argument to the
LoadPicture function)...

CommonDialog1.ShowOpen
Image1.Picture = LoadPicture(CommonDialog1.FileName)
SetImageBoxSize Image1, 150

where your ImageBox is named Image1 and the picture won't come any
closer than 150 twips to an edge of its Container. Simply assign your
picture to the ImageBox control and call this subroutine.

Private Sub SetImageBoxSize(ImageBox As Image, _
Optional ImageReductionAmount As Long = 0)
Dim ParentRatio As Single
Dim PictureRatio As Single
Dim ContainerWidth As Single
Dim ContainerHeight As Single
Dim ContainerControl As Control
With ImageBox
.Visible = False
.Stretch = False
PictureRatio = .Width / .Height
On Error Resume Next
ContainerWidth = .Container.ScaleWidth
If Err.Number Then
ContainerWidth = .Container.Width
ContainerHeight = .Container.Height
Else
ContainerHeight = .Container.ScaleHeight
End If
ParentRatio = ContainerWidth / ContainerHeight
If ParentRatio < PictureRatio Then
.Width = ContainerWidth - 2 * ImageReductionAmount
.Height = .Width / PictureRatio
Else
.Height = ContainerHeight - 2 * ImageReductionAmount
.Width = .Height * PictureRatio
End If
.Stretch = True
.Move (ContainerWidth - .Width) \ 2, _
(ContainerHeight - .Height) \ 2
.Visible = True
End With
End Sub

rick_rothstein | Wed, 30 Apr 2008 18:19:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories