Picture on Form

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
 
P

Peter T

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
 
P

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
 
K

kirkm

Hi Peter,

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

I'm new to this.

Cheers - Kirk
 
K

kirkm

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
 
P

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%

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
 
K

kirkm

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
 
P

Peter T

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
 
K

kirkm

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
 
K

kirkm

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
 
K

kirkm

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
 
P

Peter T

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
 
K

kirkm

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
 

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

Top