Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does the image not change size? 1

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
DK
I'm using the savepicture method to save a forms image to a bitmap file. During the program I'm changing the size of the form, but the size of the image does not change. Why?
How do I fix It?


Try for example:
-------------------------------------------------------
Sub main()
Form1.Width = 1000
Form1.AutoRedraw = True
MsgBox (Form1.Image.Width)
Form1.Width = 100
MsgBox (Form1.Image.Width)
End
End Sub
-------------------------------------------------------
The image has the same size even though the size of the form has changed.

Thanks in advance
Sunaj
 
Am I being stupid or does nobody know this?
 
If I understand you correctly then this would solve your problem:

- set the Stretch property of the image to true
- in the form's resize event set
Image1.Top = 0
Image1.Left = 0
Image1.Width = me.Width
Image1.Height = me.Height

This way the image stretches to the size of the form.

Herman :-Q
 
Hi,

I'm drawing directly on the form, so there's no 'Image1',
Form1.Image contains a handle to the image of the form.

The Image of the form does not stretch with the form (and it should not), but simply always have the same size as the form, but it doesn't, and I'm a bit frustrated here...

Help!
Sunaj
 

The image isn't supposed to resize itself, I'm afraid, so you have to do it yourself.Be aware that the Image property of a form is actually a read-only instance of the IPictureDisp class rather than the more normal Image class familiar in VB, so you can't directly manipulate it at run-time.

Additionally I'm not sure what you mean by "does not stretch (which it shouldn't), but simply have the same size as the form" as these two options would seem to be incompatible.

Therefore, let's try two solutions.

Firstly a method that does stretching:

you need an image control on your form (you can use a picture control instead, but it's a much heavier control and not necessary). Load your the required image into this rather than into the form either at design-time, through LoadPicture at run-time, or through assigning the image from another control at run-time. You can set the visible property to false

Then , in the forms resize event (as per Herman's solution):

Private Sub Form_Resize()
Form1.PaintPicture Form1.Image1, 0, 0, Me.Width, Me.Height
End Sub


Secondly, no stretching, but an image that reflects the size of the desktop.

This time you need a picture box (I've called in picDest) on the form. Set the borders to none.

Then, again in the resize event:

Private Sub Form_Resize()
picDest.Width = Me.Width
picDest.Height = Me.Height
picDest.PaintPicture Form1.Image, 0, 0
End Sub

And then you can use the SavePicture method against the PictureBox control.
 
Hi Strongm

Thanks for your answer.
There's definetly something here that I don't understand.

the savepicture method works on the .image property. It does not make any difference if I use a picture box or the form itself.

Let me explain my situation:
I do not load anything on the form; I draw a graph directly on the form. When everything is drawn I want to save it into a file (using savepicture). This all works very fine (and the image allways have the same size as the form (or picturebox): form1.image.width

Problem only arises when I resize the form/picurebox (to draw a new graph in a different size): The Image size never gets smaller that it was before!

Try this: take a form and set the borderstyle to none. Paste this code into the form:
------------------------------------------------------------
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Width = Int((9000) * Rnd + 1000)
MsgBox (Me.Image.Width)
End Sub
-----------------------------------------------------------

You'll se that everytime the form gets larger the number increases, but when the form gets smaller the number will not change.

This is my problem (e.i. when I want to make a smaller graph the bitmap that I save using savepicture has the same dimensions as the largest graph that I made).

Note: I do not want the image on the form to stretch as I resize the form (or rather I don't care since I'm drawing a new graph), but I DO whan the .Image to have the same size as the form/picturebox.

Sorry for the long text - I hope that you understand my problem.
Sunaj

 
Sunaj,

Thanks for the further clarification. The second of my proposed solutions should work under these circumstances.

Mike
 
Hi Strongm

No, I can't get your suggestion working. I took a form with a menu item and the following code:
------------------------------------------------------------
Private Sub Form_Load()
PicDest.Top = 0
PicDest.Left = 0
PicDest.Width = Me.Width
PicDest.Height = Me.Height
End Sub

Private Sub Form_Resize()
PicDest.Width = Me.Width
PicDest.Height = Me.Height
PicDest.PaintPicture Form1.Image, 0, 0
End Sub

Private Sub MnuSavePic_Click()
SavePicture PicDest.Image, "c:\tmp\pictest.bmp"
MsgBox ("saved")
End Sub
-----------------------------------------------------------

Started the program and made the form very big, saved the image by clicking the menu item: the bitmap is a it is supposed to be.
Then I made the form small and saved the image again: The saved bitmap has the dimensions of the first (big) image!


I still don't understand it, but I've solved the problem by unloading the form each time a make a resize (it's no problem in my case).

It seems as if memory is allocated to the image when you make i larger, but when you make it smaller the memory is not released again (before you unload the form).

Thanks for your help anyway.
Sunaj
 
Sorry, forgot a step (whilst doing a cut and paste)

Effectively, you need to reset the image size by providing a new image each time. The following achieves this:


Private Sub Form_Resize()
Dim mypic As StdPicture

Set mypic = New StdPicture
Set picDest = mypic
picDest.Width = Me.Width
picDest.Height = Me.Height
picDest.PaintPicture Form1.Image, 0, 0
Set mypic = Nothing
End Sub
 
Hi strongm

I just found out that form1.cls resets the form1.image size, which was just what I needed.

Thanks for your interest and your suggestions.
*:->*Sunaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top