Link to home
Start Free TrialLog in
Avatar of phthatcher
phthatcher

asked on

Resizing Form and PictureBox or Image to fit size of picture

I am using VB6.  What I am attempting to do is load a picture from Form1 to a picturebox or image on form2, auto resize the picturebox or image and form2 to fit the size of the picture.  

I've tried a few different ways.

Private sub cmdFullSize_Click()

    Dim TempPath           as String

   TempPath = "c:\temp\temp.jpg"

    Form2.Picture = LoadPicture(TempPath)
    Form2.Caption = TempPath
    Form2.Height = Form2.Picture.Height
    Form2.Width = Form2.Picture.Width
    Form2.Show

or using a picturebox or image

  form2.image1.picture = LoadPicture(TempPath)
  Form2.Caption = TempPath
  Form2.Image1.AutoResize = True 'sets Image1 size
  form2.height = form2.image1.height
  form2.width = form2.image1.width


And a few other ways I've tried.  With the second method the form is always a smaller length and width than the image.  With the first way the form is always taller and wider.  How can i find the exact size?  Any help would be greatly appreciated.  Thank you

End Sub
Avatar of sirbounty
sirbounty
Flag of United States of America image

Haha - small world.
I was just doing this very thing today (you weren't sitting behind me were you? :D)

In
private sub form2_load()
form2.Image1.width = form2.width
form2.Image1.height = form2.height
End Sub

unless you need something else in there...
That should do it...:D
ASKER CERTIFIED SOLUTION
Avatar of Amro Osama
Amro Osama
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of phthatcher
phthatcher

ASKER

Tried those, but to no avail.  Thanks for your posts.
What if you use the imagecontrol on form2 and set it to autosize? As I remember, the control will expand to fit the picture

then set the forms width and height to the width and height of the image on form2

Don't hesitate to ask for more help

Good luck
I've tried that as well.  The image always sizes (with autosize) correctly, but the .height and .width properties of the form when resized to an image or even the form.picture size is always off.  Using an Image or Picture the form is always wider and taller than the picture loaded.  If the picture is large then the form is quite a bit larger, if the picture is small the form is only slightly larger.  Using the form.picture property the form is always smaller than the picture.   The larger the picture the more that gets cut off.  I'm not sure quite what method will work.  I've played with .height and .width i've tried scaleheight and scalewidth made sure they were both as pixels or twip or any combination i could think of, its always just a little off.
Use

Form2.Width = Image1.width
Form2.Height = Image1.Height

Above code will make the form exactly the same size as the imagecontrol, but if you want some edges around then image, then you should add that to the forms width.

Maybe you can debug through your code and see what the values of width and height of both the form and imagecontrol are?
I will try that, I have tried it but here's for hoping anyway, thanks for the help and I will get back to you after a few cups of coffee.  Thanks
Good luck
Try this code.......

Note::
This is not my written code..... I have found it on a website for VB .NET and just changed it slightly for VB.....

Add a Command Button on Form1 and a PictureBox on Form2

On General Declaration of Form1 Paste this code


Private Sub Command1_Click()
Dim TempPath As String
TempPath = "D:\Documents and Settings\Imran\My Documents\My Pictures\falls1.jpg"

         Load Form2
         Form2.Picture1.Picture = LoadPicture(TempPath)
         Form2.Show
     
   
End Sub




'Now Paste the following in the General Declaration of Form2......... Where you have a Picture Box


Option Explicit
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

Private Const SM_CXBORDER = 5
Private Const SM_CYCAPTION = 4
Private Const SM_CYBORDER = 6



Private Sub Picture1_Change()

   Picture1.AutoSize = True
   Call AutoSizeToPicture(Picture1)
   
End Sub


Private Sub AutoSizeToPicture(pbox As PictureBox)

   Dim vOffset As Long
   Dim hOffset As Long
   Dim twipsx As Long
   Dim twipsy As Long
   
   twipsx = Screen.TwipsPerPixelX
   twipsy = Screen.TwipsPerPixelY
   
   hOffset = ((GetSystemMetrics(SM_CXBORDER) * twipsx) * 4) + 40
   
   vOffset = GetSystemMetrics(SM_CYCAPTION) * twipsy + _
           ((GetSystemMetrics(SM_CYBORDER) * twipsy) * 4) + 40
               
  'position the picture box and resize the form
   With pbox
      .Left = 0
      .Top = 0
       Me.Move Me.Left, Me.Top, .Width + hOffset, .Height + vOffset
   End With

End Sub

I'm sure all these should work, I tried your imarshad, but nothing seems to work right,  I have to hand it over to the customer so I'll just maximize the form and hope he doesn't take pictures larger than his screen.  Thank everyone for your input.
What was the problem you have occured with the code? It worked quite nicely for me on Windows XP environment......