Home All Groups Group Topic Archive Search About

Picturebox fit newbe question

Author
10 Mar 2006 8:35 AM
Kc
I created a picturebox.
I want to show different images in it.
Problem is that they all have differentt sizes.
(Most of them are .jpg)
The bitmaps must fit in the picturebox.

Problem1:
When inputting a .jpg in the picturebox, it doesn't fit.
The .jpg is smaller than the picturebox.
the rightside and bottom side are about 2-3 mm to short.

Problem2:
The bitmaps have all different in heigh en width.
How to show the bitmaps with the correct aspect ratio ?? (Heigh and width)


Routine i use to test:

Public Sub picShowPicture(oPictureBox As Object, _
  ByVal sFile As String, _
  Optional ByVal bStretch As Boolean = True)

  With oPictureBox
    If bStretch Then
      .AutoRedraw = True
      Set .Picture = Nothing
      .PaintPicture LoadPicture(sFile), 0, 0, .ScaleWidth, .ScaleHeight
      .AutoRedraw = False
    Else
      Set .Picture = Nothing
      .Picture = LoadPicture(sFile)
      .AutoSize = True
    End If
  End With
End Sub

Private Sub Command2_Click()
    picShowPicture Picture2, "c:\Test.jpg"
End Sub


--------------=  Posted using GrabIt  =----------------
------=  Binary Usenet downloading made easy =---------
-=  Get GrabIt for free from http://www.shemes.com/  =-

Author
10 Mar 2006 8:48 AM
Rick Rothstein [MVP - Visual Basic]
> I created a picturebox.
> I want to show different images in it.
> Problem is that they all have differentt sizes.
> (Most of them are .jpg)
> The bitmaps must fit in the picturebox.

Perhaps the following previous post of mine will be of some help. What the
subroutine does is fit a picture to the display container while maintaining
the pictures aspect ratio.

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
Author
10 Mar 2006 8:52 AM
NickHK
Kc,
An Image control can resize the graphic to fit its control.
Set the .Stretch property.

If you need to use a picture box,
http://www.vb-helper.com/howto_fit_picture_to_box.html

NickHK

Show quoteHide quote
"Kc" <Kc@nospam.nl> wrote in message
news:oTaQf.36335$_B2.20070@fe72.usenetserver.com...
> I created a picturebox.
> I want to show different images in it.
> Problem is that they all have differentt sizes.
> (Most of them are .jpg)
> The bitmaps must fit in the picturebox.
>
> Problem1:
> When inputting a .jpg in the picturebox, it doesn't fit.
> The .jpg is smaller than the picturebox.
> the rightside and bottom side are about 2-3 mm to short.
>
> Problem2:
> The bitmaps have all different in heigh en width.
> How to show the bitmaps with the correct aspect ratio ?? (Heigh and width)
>
>
> Routine i use to test:
>
> Public Sub picShowPicture(oPictureBox As Object, _
>   ByVal sFile As String, _
>   Optional ByVal bStretch As Boolean = True)
>
>   With oPictureBox
>     If bStretch Then
>       .AutoRedraw = True
>       Set .Picture = Nothing
>       .PaintPicture LoadPicture(sFile), 0, 0, .ScaleWidth, .ScaleHeight
>       .AutoRedraw = False
>     Else
>       Set .Picture = Nothing
>       .Picture = LoadPicture(sFile)
>       .AutoSize = True
>     End If
>   End With
> End Sub
>
> Private Sub Command2_Click()
>     picShowPicture Picture2, "c:\Test.jpg"
> End Sub
>
>
> --------------=  Posted using GrabIt  =----------------
> ------=  Binary Usenet downloading made easy =---------
> -=  Get GrabIt for free from http://www.shemes.com/  =-
>
>