Picture on Form

  • Thread starter Thread starter kirkm
  • Start date Start date
K

kirkm

I'm displaying a picture on a Form and
using the zoom property to adjust the size.

The problem is this resizes the picture but not the form.

Is there a method to force the form to the same size as the picture.

Or, can I display just the picture without a form? There is
one command button in use on the form as well.

Thanks - Kirk
 
Hi Kirk,

Why not use an image control with its Autosize property set to true, resize
the form to the image, eg

sFile = "c:\pictures\mypicture.jpg"
Me.Image1.Picture = LoadPicture(sFile)

With Me.Image1
..AutoSize = True ' or set at design time
..Left = 0
..Top = 0
Me.Width = .Width
Me.Height = .Height
End With

You might also want to re position the form to centre in the monitor, or
even re-scale if the image doesn't fit. So might need the GetSystemMetrics32
api to get the screen resolution.

Regards,
Peter T
 
I forgot would need to increase the form's height by same as its title bar
height

Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long
Const SM_CYCAPTION = 4

CaptionHeight = GetSystemMetrics(SM_CYCAPTION)

Other constants you might need to return dimensions from GetSystemMetrics

Const SM_CXSCREEN = 0 ' screen pixel width
Const SM_CYSCREEN = 1 '' screen pixel height
Const SM_CYMENU = 15 taskbar height
Const SM_CXFULLSCREEN = 16
Const SM_CYFULLSCREEN = 17

Then need to convert from pixels to points by multiplying by factor 0.75 (ie
typically assuming not large system font).

Also, this simplified method might trim the image slightly as borders are
not taken into account.

Regards,
Peter T
 
Hi Peter,

Thanks for the examples. I'm still playing around with them.

I'm new to this.

Cheers - Kirk
 
Peter, I'm getting the same effect with the
image control as the picture. When I zoom
e.g. to 75 the image reduces, but the form stays
the initial size.
What I'm hoping to achieve is have the picture
and form resize together.
Zoom is good because it allows both
smaller and larger resizing.

Thanks - Kirk
 
I'm not quite sure what you are trying to do, perhaps something like this -

Userform with image control named Image1
change the picture file name
click on image to change zoom +/-25%

Option Explicit
Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long
Const SM_CYCAPTION = 4

Dim mWd As Long, mHt As Long
Dim mCapHt As Long
Dim mInc As Long
Dim mZm As Long

Const cBDR_WD = 4 ' approx image border width

Private Sub Image1_Click()
If mZm > 125 Then mInc = -1
If mZm < 50 Then mInc = 1
mZm = mZm + (25 * mInc)
Me.Zoom = mZm
Me.Width = Int(mWd * mZm / 100) + cBDR_WD
Me.Height = Int(mHt * mZm / 100) + mCapHt

Me.Caption = mZm & "%"
End Sub

Private Sub UserForm_Initialize()
Dim sFile As String
sFile = "c:\pictures\myPicture.jpg" ' >==CHANGE

mCapHt = GetSystemMetrics(SM_CYCAPTION)

With Me.Image1
.AutoSize = True
.Left = 0
.Top = 0
.Picture = LoadPicture(sFile)
mWd = .Width
mHt = .Height
End With

Me.Top = 0
Me.Width = mWd + cBDR_WD
Me.Height = mHt + mCapHt

mInc = -1
mZm = 100
End Sub

Regards,
Peter T
 
I'm not quite sure what you are trying to do, perhaps something like this -

Userform with image control named Image1
change the picture file name
click on image to change zoom +/-25%


Hi Peter,

That's absolutely brilliant. Many thanks indeed !

I was quite pleased I managed to work outy where everything should go
and get it working! Had my doubts to start with...

Just 2 things remain (which I may be able to figure) - a default
size that it always starts with, although I suspect this is now
dependant on the picture size, and they aren't all the same.
And, can a command button be placed on the picture, caption "Next' ?
Or would this need to go somewhere else?
Sometimes there's more than one associated image. A public variable
keeps track of that.

Many thanks, I got in over my depth a bit here but sort of know what
I want, just not how to achieve it.
Cheers - Kirk
 
Hi again Kirk,

Some pointers.

Zoom changes all controls in the container, so if your buttons are in the
same container they'll change size, but could use a frame which is also a
container. Try this

Put one or two buttons at the top of the form (.top = 0) make both same
height

Add a frame named Frame1, with the frame selected add Image1

Extend the code to change dimensions of Frame1 to same as the picture but
with its top = 0 + button. Also in all size/zoom changes make height of form
= Frame1.height (previously adjusted from the Image1.height) + button.height
+ titlebar.height (from the API). Perhaps add a little extra to both height
& width to allow for picture & frame borders.

Change the zoom to Me.Frame1.Zoom = z

Might want to restrict minimum form width to that of the right edge of the
rightmost button, eg minFrmWidth = button2.left + button2.width +
a-little-bit.

In the previous example, make most of the code in the Initialize event a
dedicated function that receives a string filename. Code in the initalize
event something like

sFile = "C:\mypicture.jpg"
Call fnSetPicture sFile

You can now call the same function from another button to load new pictures.

To start with a default size:
Decide if the default will be the X or Y dimension, let's say X
After running fnSetPicture compare loaded width with defaultX, eg
factor = Int(defaultX/Image1.Width * 100)
Me.Frame.Zoom = factor

store the factor as the current zoom setting

Look at Zoom in help, gives ideas to add a scrollbar or spin to adjust zoom
rather than the +/-25% in the example.

A different approach would be not to use Zoom at all but scale the image's
width & height (and resize form to suit).

Regards,
Peter T
 
Hi Peter,

Thanks for all that info. I've spend many
hours but not really getting there.
I'll keep at it though !

Cheers - Kirk
 
Hi Peter

What's not working

Me! I don't know enough to properly implement your suggestions. So
there's a couple of us trying to figure it out. Mainly the preset
size, and Next button.

Can I ask you more after we've made a little more progress?

Thanks - Kirk
 
What's not working

Hi Peter,

Wonder if your're seeing this thread still?
Still working on it this end!

From your previous examples, the make smaller/larger
abilty is working great. It's the starting size that varies
so much - because the images themselves vary.

Is it possible to 'auto-zoom' to a set size first. Can the
resolution be calcualted from the LOF, or is the degree
of compression a factor that spoils that?

Thanks - Kirk
 
Still at it said:
Wonder if your're seeing this thread still?

I tend to keep threads for about 2 wks before deleting so still here!
Is it possible to 'auto-zoom' to a set size first.

Some good stuff here to read image size from file, "File Info" section
http://edais.mvps.org/Code/Libraries/index.html

You might find this a little daunting and not sure necessary for your
purposes (you'd need to know how to implement 'Class', the examples are
intended to drop straight into a VB6 project though at a glance I expect
would work with minimal modification in VBA).

But I think all you need to do is load the picture to its default size as
you've been doing then compare image width (X) & height (Y) with your
default dimensions. If either X or Y is too big/small, calculate the factor
of both differences and resize the picture by the largest factor. Perhaps
you might have default min & max dim's and only adjust if either X or Y is
too small/big.

As I think I mentioned before you can either use the Zoom method or rescale
the dimensions of the image. With the rescale method you might need to
change image properties Autosize True (on load) <> false (after loading) and
possibly PictureSizeMode (not saying you will, I haven't tested).

The above would be pretty much the same as the code you are already using
for user to resize (zoom) the image except you do this on loading the image
(if necessary). You'd just need to store the resize factor as the current
zoom setting.

Regards,
Peter T
 
Hi Peter,

Well after many weeks of attempting this task I've decided to go back
to my first image idea and scrap the Image control and any fancy
zoom techniques. I just can't make it work. I'll put two < > buttons
on the form that can allow a
set size, one larger, one smaller. I can also put a Next button on top
of the picture. And with a bit of luck, figure all that out. I did
have that almost working at one stage.


At the moment though both these lines -

frmLabel.Show vbModeless
frmLabel.Picture = LoadPicture(LName(1))

Bring up the same error.

Run-time error '-2147352573 (80020003)':
Could not find the specified object.


Any idea what might be wrong - what 'object' it's looking for?
(I do have a frmLabel).


Thanks - Kirk
 
I thought you almost had it!

Have a go with this, pretty much along the lines I tried to suggest earlier,
with rescale method rather than frame zoom).

View pictures in a folder

Populates an array with picture files in msPath
If picture too large reduces to max default size
Left click the image +/- 25% original size
Double click image to reset default size
Next button to load next picture

'UserForm StartUpPosition 0 Manual
'Image1 AutoSize True
' PictureSizeMode 3 frmPictureSizeModeZoom
'CommandButton1 caption "Next"
'
'change sPath to some picture folder (in the Initialize event),
'
'Change DEFWIDTH & DEFHEIGHT to suit
' eg can get monitor res' from an API
' then typically need to reduce by 75%

Option Explicit
Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long
Const SM_CYCAPTION = 4

Dim mWd As Long, mHt As Long
Dim mCapHt As Long
Dim mnCurrentPic As Long
Dim mButtonBottom As Long
Dim mInc As Long
Dim mdblZoom As Double
Dim mDefScale As Double
Dim msPath As String
Dim masPics() As String

Const cBDR_WD = 4 ' approx image border width
Const DEFWIDTH = 800 * 0.75
Const DEFHEIGHT As Long = 600 * 0.75

Private Sub CommandButton1_Click()
'Next button
If mnCurrentPic = UBound(masPics) Then
mnCurrentPic = 0
Else
mnCurrentPic = mnCurrentPic + 1
End If
LoadPic msPath & masPics(mnCurrentPic)
End Sub

Private Sub Image1_Click()
mdblZoom = (mdblZoom * 100 \ 25) * 0.25

If mdblZoom > 1.25 Then mInc = -1
If mdblZoom < 0.5 Then mInc = 1

mdblZoom = mdblZoom + (0.25 * mInc)

Resize
End Sub

Private Sub Image1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
mdblZoom = mDefScale
Resize
End Sub

Private Sub UserForm_Initialize()

msPath = "C:\Pictures\"
'sPath = "C:\My Documents\My Pictures\"

With Me.CommandButton1
.Caption = "Next"
.Top = 0
.Left = 0
End With

If GetPics(msPath) Then
MsgBox "Error getting picture files"
Else
mCapHt = GetSystemMetrics(SM_CYCAPTION) * 0.75
With Me.CommandButton1
mButtonBottom = .Top + .Height
End With

LoadPic msPath & masPics(0)
End If

End Sub

Function GetPics(sPath As String) As Long
Dim sFile As String
Dim n As Long
Dim i As Long
Dim arr

arr = Array("*.jpg", "*.gif", "*.bmp")
ReDim masPics(0 To 100)
On Error GoTo errH
n = -1
For i = 0 To UBound(arr)
sFile = Dir(sPath & arr(i))
Do While Len(sFile)
n = n + 1
masPics(n) = sFile
sFile = Dir()
Loop
Next

ReDim Preserve masPics(0 To n)
Exit Function
errH:
If n > UBound(masPics) Then
ReDim Preserve masPics(0 To UBound(masPics) + 100)
Resume
End If
GetPics = Err.Number

End Function

Sub LoadPic(sFile As String)
Dim xDiff As Long, yDiff As Long

With Me.Image1
.AutoSize = True
.Left = 0
.Top = mButtonBottom
.Picture = LoadPicture(sFile)
mWd = .Width
mHt = .Height
End With

Me.Top = 0
Me.Left = 0
Me.Width = mWd + cBDR_WD
Me.Height = mHt + mCapHt + mButtonBottom

mInc = -1

With Me
xDiff = .Width - DEFWIDTH
yDiff = .Height - DEFHEIGHT
End With

If xDiff > 0 Or yDiff > 0 Then
If xDiff / DEFWIDTH > yDiff / DEFHEIGHT Then
mDefScale = 1 - (xDiff / mWd)
Else
mDefScale = 1 - (yDiff / mHt)
End If

mdblZoom = mDefScale
Resize
Else
mDefScale = 1
Me.Caption = "100% " & masPics(mnCurrentPic)
End If
Me.Caption = Me.Caption & " " _
& mnCurrentPic + 1 & "\" & UBound(masPics) + 1
End Sub

Private Sub Resize()

With Image1
.Width = Int(mWd * mdblZoom)
.Height = Int(mHt * mdblZoom)
End With

'might want to restrict min width to right edge of rightmost button
Me.Width = Int(mWd * mdblZoom) + cBDR_WD
Me.Height = Int(mHt * mdblZoom) + mCapHt + mButtonBottom

Me.Caption = Int(mdblZoom * 100) & "% " & masPics(mnCurrentPic)
End Sub

Some of the dimension allowances are not quite right but I think near enough
for a light demo.

Regards,
Peter T

PS
Run-time error '-2147352573 (80020003)':
Could not find the specified object.

probably 'LName(1)' not path & name of an image file
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Image Control Question 5
Picture property 2
LoadPicture fails 3
Image Property 2
Zoom in/Zoom out picture 1
Resize comments with only picture. 6
To insert a picture in a form 1
User Form 7

Back
Top