Link to home
Start Free TrialLog in
Avatar of Jacques Geday
Jacques GedayFlag for Canada

asked on

VB6 Image/Picture handling

Hello,

I have a Form that has following Controls:
1) either an Image Control or a PictureBox control (Whichever is more suitable for my usage) called Image1
2) An Horizontal scrollbar called HScrollbar1
3) A Vertical scrollbar called VScrollbar1

The main purpose of the Form is to display an Image in the size of the form when the user doubleclik on my project's smaller picturebox. My main purpose is to enlarge a picture in the Form and I would like to acheive the following:

1) I need to be able to zoom In and Out of the picture (what is the code for that)
2) I need to have the Picture Fit the Form ie if I maximize the form the picture need to fit the entire Form and vice versa when I resize the form the picture need to be resized. (What is the code for that)
3) I need Both scrollbar to be able to navigate and see the picture from left to extreem right and from top to bottom. I have diffrent picture sizes I always need the Max in both scrollbar to navigate to the Max size image wether width wyse or height wyse.  (Also what is the code for that).
4) I need to be able to also Print The picture and Copy it to clipboard if necessary and what is the code for that.

My findings so far showed me that in the Image control there is a property that is called Stretch that will squeeze the pic to fit the Image control size which is not available in the PictureBox what is the alternative for that in picture box and which control is better for my usage.

Tks/Rgds
gowflow
Avatar of BrianVSoft
BrianVSoft
Flag of Australia image

You can have two imageboxes,
Image1 is visible with "Stretch" set to true..
Image2 is hidden with "Stretch" set to false - Image2 will resize itself to the actual image dimensions which you can copy into variables so you know the ratio of width to height.
You then use that ratio factor to control the visible Image1 as you zoom it larger or smaller by simply setting its Image1.Height and Image1.Width.
You can use Scroll bars to move it around when it is bigger than the form.. (see code below)
It looks better if Image1 is on a Frame with the scroll bars below and beside the frame..

You can set the image by..
Image1.Picture = LoadPicture("c:\MyImage.jpg") ' Etc.

You can copy the image to Image2 by..
Image2.Picture = Image1.Picture

To Print an image, it needs to be copied to a PictureBox - then use the PaintPicture command as shown below.. Lookup the normal help on PaintPicture.
Sub HorizScroll_Change()
   Image1.Left = -HorizScroll.Value * Factr4
End Sub

Sub VertScroll_Change()
  Image1.Top = -VertScroll.Value * Factr4
End Sub

Printer.PaintPicture PicBox1.Picture, x1 , y1
' x1,y1 are the twip positions of the top left corner

Open in new window

Avatar of Jacques Geday

ASKER

BrianVSoft,

Tks your input which is much appreciated. Good idea the two images ! I testing this right now but hv a small issue about the factor you mention:

Image2 will resize itself to the actual image dimensions which you can copy into variables so you know the ratio of width to height.
>>> How you get the ratio ? is it Image2.Width/Image2.Height or ???  I guess its Factr4 your talking about !!!

You then use that ratio factor to control the visible Image1 as you zoom it larger or smaller by simply setting its Image1.Height and Image1.Width.
>>> Pls give an example !!!

Rgds/gowflow
1. Yes, Ratio of Width to Height is simply AspHW = Image2.Width/Image2.Height - You use that whenever you set the height and width of Image1.. Eg. As you Zoom in or out by calculating a New-Height, you set Image1.Width = New-Height * AspHW (the zoom could be done by Up & Down buttons or via another Scroll Bar)
2. Factr4 is only used in the HorizScroll and VertScroll which move the whole Image1 around.
Factr4 is different for both scrollbars (FactrH4 and FactrV4) These are re-calculated at the time you re-set Image1 size. (Zoom in or out)
FactrH4 is calculated so that the Image1 moves full left to full right as HorizScroll moves from one extreme to the other. You need to experiment with it..
Assuming Image1 is on Frame1, FactrH4 is roughly calculated from..  OverLap =  Image1.Width - Frame1.Width (the amount of width overlap) (in fact, if that is negative, you can hide that Scroll bar.)
OverLap is then divided by HorizScroll.Max to give FactrH4. Same logic for FactrV4
Avatar of puppydogbuddy
puppydogbuddy

Just a FYI: Stephen Lebans web site has a lot of free useful image/picture handling functions (code) implemented in VB6 for an MS Access 2000 database that Access developers have been using for years.  I have provided links to three of them as examples.

http://www.lebans.com/imagecontroltoclipboard.htm
is a database containing a function that allows you to copy the contents of a standard Image Control to the ClipBoard.

http://www.lebans.com/autosizeole.htm
is a database containing functionality to allow for the auto sizing of bound or unbound OLE Frame controls.

http://www.lebans.com/customscrollbar.htm
is a database containing functions to simulate a Standard ScrollBar control. Now fully functional!
It is built directly in native Access. No calls to API or use of Timers.
Sorry you lost me !!

Please lets focus one point by one point. I am now intrested to fix the scrollbar issue here is what I got:

1) I see no puropose for having 2 images as when Image1 is set to Stretch then no need for scrollbars as the picture is fitting its dimention all the time. So I have set Image1 to Stretch = False

2) Sake of the example I kept Image2 Not visible and Stretch = False and I have following in my Form Load:
HScroll1.Max = Image2.Width         ' Set maximum value.
HScroll1.LargeChange = HScroll1.Max / 5   ' Cross in 5 clicks.
HScroll1.SmallChange = HScroll1.Max / 20   ' Cross in 20 clicks.
VScroll1.Max = Image2.Height     ' Set maximum value.
VScroll1.LargeChange = VScroll1.Max / 5   ' Cross in 5 clicks.
VScroll1.SmallChange = VScroll1.Max / 20   ' Cross in 20 clicks.
Image1.Left = 0   ' Start picture at left.
Image1.Refresh

Then I have this Also but it seems not to work and need your input
Private Sub HScroll1_Change()
' Move picture according to scroll bar.
Image1.Left = -(HScroll1.Value / HScroll1.Max) * ScaleWidth
Image1.Refresh
End Sub

Private Sub VScroll1_Change()
' Move picture according to scroll bar.
Image1.Top = -(VScroll1.Value / VScroll1.Max) * ScaleHeight
Image1.Refresh
End Sub

the picture I see is part of the whole picture and when I move the scrollbar I do not see the rest of the picture but the Grey background of the form !!! I guess My Max for both HScroll and VScroll are not correct !!! How to get them so I can get the Exact real dimentions of the picture !!!

Rgds/gowflow
If you don't need the image to zoom bigger than the form or frame size then you don't need scrollbars..
In your question, you said you needed to zoom in.. And, you needed scrollbars.. That implies you wanted to zoom bigger than form size.
You also don't need two Image boxes if you don't need to preserve the aspect ratio (Height to Width ratio)
It sounds like all you need is a Stretch Image box that resizes itself to fit the form whenever the form is resized.
You can do that in Form_Resize..
Eg.
Private Sub Form_Resize()
  If Me.WindowState = 1 Then Exit Sub ' Form is Minimizing..
  Image1.Width = Me.Width - 300: Image1.Height = Me.Height - 700
 'Note.. The 300, 700 is varied to avoid other things on the form

It sounds like you don't need the scrollbars at all?
YES I DO NEED scrollbars for sure !!!! I need to see the whole image as it is some 5MB and its width is 38800 and its height is 29960 so When I put these into HScroll1.Max = Image2.Width I get Overflow !!!
 You need to answer me in the above case of dimentions what would be
HScroll1.MAX
VScroll1.MAX
and the code for both
Private Sub HScroll1_Change()
Private Sub VScroll1_Change()

Hope its clearer now.
Rgds/gowflow
Before you proceed any further.. Try an Imagebox with stretch = true.. It will show you the full image no matter how big the image is..
From what you have said, I see no reason for Scrollbars.
If you do need them, then I should write you a working example (your code bits are not on the right track..  HScroll1.Max does not need to be = Image2.Width)
Yes correct actually in my project I have the image set to Stretch = true then I see the whole image and the image I hv in the the projject is a small fram some 1inch per 1inch.

When the user doubleclikck on this image I launch My form that has the Image1 and to which I pass ImageW = Image2.width (from my project where Image2 is hidden and Stretch = false) and also I capture ImageH = Image2.Height these 2 variable are global variable that I can use in my FImage Form where I need to have all the funcionalities described in this question.

I know that my code is not on the right track and pls go ahead and let me know the correct one as really stuck now not able to view the whole picture.

gowflow
puppydogbuddy: Tks your link sorry not replying earlier, but the links you provided does not help my case directly as included in a database. As for clipboard copy I gues its too ellaborate and presume there must be a more simpler approach. Tks your effort.
Rgds/gowflow
The code s included in access mdb file to demo that it works.  You or anyone else can go to the code editor and extract the source code and use it in your application.
Well I looked at CustomScrollBarVer8.zip and yes there is an mdb that has old format database first of all then it displays a vertical scrolbar and couple of figures displayed. Honestly not familiar with Access as specifically stated in my question VB6 !!!

If you can answer to my specific questions in providing code for my  questions then pls be welcomed.
gowflow
BrianVSoft

If you do need them, then I should write you a working example (your code bits are not on the right track..  
>> Pls go ahead and advise a working example code.

Thank you
gowflow
gowflow,
As stated in my original post, the link to the Leban's web site was provided to you as a free vb6 code resource (vb6 code was used for pre-2007 versions of ms access) for many of the functions you want to do with image/picture handling.  It is up to you to decide whether you want use any of the code that is available.
Try this before you think about ScrollBars..
If you have.. ImageW = Image2.width and  ImageH = Image2.Height
Then the ratio of Width to Height is simply AspHW = ImageW / ImageH
You use AspHW whenever you set the height and width of the "Stretch" Image1

Eg. Set Image Height to Form height.. (this is normally done in form resize as mentioned above)
 Image1.Height = Me.Height - 700
 Image1.Width = Image1.Height * AspHW: ' Set image to have same ratio.
 Me.Width = Me.Height * AspHW: ' Set form to have same ratio as image.

If you set this form to be resizeable, the user can resize the image by just dragging the form edge.
Sorry it is not working at all. with this ration the picture looks stretch-up !!! does Image1.Stretch need to be True or False ? if True then it is not serving the purpose !!!!! as I need the image to be AS IS originally and the user to navigate thru scrollbars to see the whole image up and down left and right.

It seems you did not catch the essence of what I need as you keep on repeating no scrollbars and stretch the image.

Pls understand that I developp software for over 30 years and not a newbie by no means, but just new at this issue of handling images. If you are not able to assist as requested pls let me know order avoid waist of time.
rgds/gowflow
I think you just had a small bug..
I ran the exact code below and it works great - even for 10meg images.
The Aspect ratio remains correct.
I have added a third imagebox "ImageThumb" to act as a Thumb image that you can click on to see it Max size on the form. You can resize the form to make the image full screen if necessary..

ImageThumb and Image2 are NOT 'Stretch' - Image1 IS 'Stretch'
Place Image1 at top left.. It will expand from that point.
Keep Image2 visible during testing.
You dont need to pass both Image2.Width &  Image2.Height to your big form - just the ratio AspHW
Private AspHW

Private Sub ImageThumb_Click()
  Image2.Picture = ImageThumb.Picture
  Image1.Picture = ImageThumb.Picture
  AspHW = Image2.Width / Image2.Height
  
  Image1.Height = Me.Height - 900
  Image1.Width = Image1.Height * AspHW ' Set image to have same ratio.
  Me.Width = Me.Height * AspHW: ' Set form to have same ratio as image.
End Sub

Private Sub Form_Resize()
 If Me.WindowState = 1 Or AspHW = 0 Then Exit Sub ' Form is Minimizing..
 Image1.Height = Me.Height - 900
 Image1.Width = Image1.Height * AspHW ' Set image to have same ratio.
End Sub

Open in new window

If the above code works - but you still want to zoom greater than full screen size - you will need ScrollBars.. They would only need a few lines of code..
PS.. ImageThumb IS Stretch - so that it can stay tiny.
If an imagebox is NOT Stretch, it auto resizes to the actual size of the Image.
Sorry but seems we do not understand each others. THE CODE DOES NOT WORK !!!!!
I am not talking about zoom but about scrolling left/right up/down to see the whole pic.
Pls provide lines of code so scrollbar works
gowflow
ASKER CERTIFIED SOLUTION
Avatar of BrianVSoft
BrianVSoft
Flag of Australia 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
With both the above Scrollbars, set Max = 100 : Min = -1 and LargeChange = 10
The -1 min is to provide a thin line when the slider is at zero to visually confirm that the edge of the image has been reached.
The line..
Private ImgHi, ImgWi, Hfactr, Vfactr
will need to be Public if you have this logic split over two forms.

When you click on the ImageThumb - the Image2 will fill out to its normal size on the Frame1
YES ! much better tks.
I have the following problem now:

When I scroll the image at the end right and end bottom I always go over the image edge I have some 30% of the width and 20% of the height showing the grey of the form. How can I set it so the max scroll always end up at the edge of the picture wether horzontal or vertical ?

Obviously I had to add code in the resize form as shown
Rgds/gowflow
VScroll1.Top = 0
HScroll1.Left = 0
Frame1.Left = 0
Frame1.Top = 0

Frame1.Width = Me.Width - 2 * VScroll1.Width
Frame1.Height = Me.Height - 3.2 * HScroll1.Height

VScroll1.Left = Me.Width - 2 * VScroll1.Width
VScroll1.Height = Me.Height - 3.2 * HScroll1.Height

HScroll1.Width = Me.Width - 2 * VScroll1.Width
HScroll1.Top = Me.Height - 3.2 * HScroll1.Height

Open in new window

I got it !!!
You need to add all the code like this. It will elliminate the white at the end of the image and scrolling will stop the edge of the image.

Sub HScroll1_Change()
Hfactr = ImgWi - Frame1.Width
If Hfactr < 900 Then Hfactr = 900
Image2.Left = -HScroll1.Value * Hfactr / 100
end sub

Same for  and HScroll1_Scroll()

Private Sub VScroll1_Scroll()
Vfactr = ImgHi - Frame1.Height
If Vfactr < 900 Then Vfactr = 900
Image2.Top = -VScroll1.Value * Vfactr / 100
End Sub

and same for VScroll1_Change()

Now a new problem:
What if the image is smaller than the Form I need to have the form not resize more than the edge of the image !!  Any idea ???

gowflow
SOLUTION
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
PERFECT !!!

Now that we are over with scrolling and picture fitting the whole window item 2 and 3 of my question lets focus on the zoom issue.

You mentioned in an earlier post:
=====
1. Yes, Ratio of Width to Height is simply AspHW = Image2.Width/Image2.Height - You use that whenever you set the height and width of Image1.. Eg. As you Zoom in or out by calculating a New-Height, you set Image1.Width = New-Height * AspHW (the zoom could be done by Up & Down buttons or via another Scroll Bar)
=====

I have created an updown button called Updown1 and do not quite understand your post (you set Image1.Width = New-Height * AspHW what is New-Height ??) need you to please advise what is the Max what is the Min and how to set the values and where in what Sub so when I click up I zoom In and when I click down I zoom out and the picture Zooms In or Out.

Tks your help
gowflow
You can only 'zoom-in' with a Stretch type Imagebox  Eg. Image1
When you first set the image in Image1, you saved the Height and Width in ImgHi & ImgWi..
Eg.   ImgHi = Image2.Height: ImgWi = Image2.Width
You also saved the Aspect ratio..
  AspHW = Image2.Width / Image2.Height  or =  ImgWi / ImgHi
All your Up/Down button events need to do is Increase or Decrease ImgHi  and re-set the Image1 size using that new ImgHi  as follows..
  ImgWi =  ImgHi  * AspHW
  Image1.Height = ImgHi : Image1.Width = ImgWi
 Also in this UpButton_Click event, you have to repeat your lines that set Hfactr & Vfactr for the scrollbars.. Eg.
  Hfactr = ImgWi - Frame1.Width: If Wfactr < 900 Then Wfactr = 900
  Vfactr = ImgHi - Frame1.Height: If Vfactr < 900 Then Vfactr = 900


I don't follow your question "what is the Max what is the Min" - what is that referring to?
I don't follow your question "what is the Max what is the Min" - what is that referring to?
>>> When I created the control Updown1 it has 2 property Min=0 and Max=10 by default should I keep those as is or change them ?

Rgds/gowflow
You can leave them as is if you dont want to zoom smaller..
Min could be -10 if you want to zoom smaller.
In Sub UpDown1_Change(),, you have to work out a good math factor so that the zoom steps are smooth and practical (that usually involves a log function)
but you can just start with an experiment like..
Sub UpDown1_Change()
 NewImgHi = ImgHi * (1 + UpDown1.Value / 5)
 (that will go from unity to zoom X3 in 10 steps - then play with the 5)
NewImgHi = ImgHi * (1 + UpDown1.Value / 5)
>>> What do I do with NewImgHi ? Image2.height = NewImgHi ???
Should I also have there NewImgWi = ImgWi * (1 + UpDown1.Value / 5) ????

Can I zoom In and Out on Image2 that is  Unstretched ?? as if on Image1 then it is not visible on my frame only Image2 is !!!

gowflow
SOLUTION
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
YES YES YES !!!!
My freind your a genius !!! tks so much !!! I had to get the /5 to /20 and my Min = -30 Max = +30 to have a nice Zooming !!! So now item 1 of our question is also gone we are only left with item 4 the printing !!

Sorry to be a pain, but I am as new to the picture/image issue as a new born who doesn't know how to speak, eat, talk ... u name it !!1

To go back to our subject I know you mentioned it earlier ... but need now to focus on instructions to:
Print Image1 or Image2 to the printer (you had earlier mentioned Printer.PaintPicture PicBox1.Picture, x1 , y1 ' x1,y1 are the twip positions of the top left corner) how would you do that practically knowing our variables and case !!


After that ... I will set you free !! Tks for all your help and sorry for earlier post as flet we were spinning around without results.

Rgds/gowflow
SOLUTION
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
Tks V much for the patience and great advice