Solved

VB6 Image/Picture handling

Posted on 2011-03-07
33
902 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:gowflow
  • 16
  • 14
  • 3
33 Comments
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35067891
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

0
 
LVL 29

Author Comment

by:gowflow
ID: 35068338
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
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35068569
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
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 35068785
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.
0
 
LVL 29

Author Comment

by:gowflow
ID: 35068854
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
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35069097
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?
0
 
LVL 29

Author Comment

by:gowflow
ID: 35069268
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
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35069580
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)
0
 
LVL 29

Author Comment

by:gowflow
ID: 35069648
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
0
 
LVL 29

Author Comment

by:gowflow
ID: 35071001
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
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 35071293
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.
0
 
LVL 29

Author Comment

by:gowflow
ID: 35071357
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
0
 
LVL 29

Author Comment

by:gowflow
ID: 35072149
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
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 35072990
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.
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35074769
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.
0
 
LVL 29

Author Comment

by:gowflow
ID: 35080230
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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35080587
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

0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35080859
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.
0
 
LVL 29

Author Comment

by:gowflow
ID: 35082372
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
0
 
LVL 5

Accepted Solution

by:
BrianVSoft earned 500 total points
ID: 35083887
This version doesn't use Image1 (Stretched), Just ImageThumb and Image2
Image2 is Non Stretch so it can size to very big.. It sits on Frame1 which may be smaller than Image2 so needs scrollbars..
Set the H.scrollbar below the frame and Vscrollbar beside the frame.
When you click on the ImageThumb - the Image2

This code works well for all image sizes..
The previous code also worked great - ALL image sizes could be seen in full without scrollbars.. In future, please don't just say "THE CODE DOES NOT WORK !!!!" - Explain what aspect doesn't suit your purpose and why.

Private ImgHi, ImgWi, Hfactr, Vfactr

Private Sub Form_Load()
 ImageThumb.Picture = LoadPicture("D:\B\Test2.jpg")
End Sub

Private Sub ImageThumb_Click()
  Image2.Picture = ImageThumb.Picture
  ImgHi = Image2.Height: ImgWi = Image2.Width
  Hfactr = ImgWi - Frame1.Width: If Wfactr < 900 Then Wfactr = 900
  Vfactr = ImgHi - Frame1.Height: If Vfactr < 900 Then Vfactr = 900 ' This 900 sets a reasonable scroll speed when the Image is only a few percent too big.. 
End Sub


Private Sub HScroll1_Change()
  Image2.Left = -HScroll1.Value * Hfactr / 99
End Sub

Private Sub HScroll1_Scroll()
  Image2.Left = -HScroll1.Value * Hfactr / 99
End Sub

Private Sub VScroll1_Change()
  Image2.Top = -VScroll1.Value * Vfactr / 99
End Sub

Private Sub VScroll1_Scroll()
  Image2.Top = -VScroll1.Value * Vfactr / 99
End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35084019
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
0
 
LVL 29

Author Comment

by:gowflow
ID: 35085750
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

0
 
LVL 29

Author Comment

by:gowflow
ID: 35086151
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
0
 
LVL 5

Assisted Solution

by:BrianVSoft
BrianVSoft earned 500 total points
ID: 35088645
You would use the ImgHi & ImgWi variables that you set at this line..
 ImgHi = Image2.Height: ImgWi = Image2.Width
At Form_Load, you would test to see if the Form was bigger than these and reset Me.Height and Me.Width.
Eg.. (In Form Load)
MinWid =  ImgWi + 200 : If Me.Width > MinWid Then Me.Width = MinWid
MinHi =  ImgHi + 200 : If Me.Height> MinHi Then Me.Height= MinHi
0
 
LVL 29

Author Comment

by:gowflow
ID: 35093454
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
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35093602
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?
0
 
LVL 29

Author Comment

by:gowflow
ID: 35093766
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
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 35093901
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)
0
 
LVL 29

Author Comment

by:gowflow
ID: 35093977
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
0
 
LVL 5

Assisted Solution

by:BrianVSoft
BrianVSoft earned 500 total points
ID: 35094117
No.. You can only 'zoom-in' with a Stretch type Imagebox  Eg. Image1
You can easily change Image2 to a Stretch type - the only difference is that you have to manually set its Height and Width - which you are already have in ImgHi & ImgWi..

With NewImgHi..
Sub UpDown1_Change()
  NewImgHi = ImgHi * (1 + UpDown1.Value / 5)
  NewImgWi =  NewImgHi * AspHW
  Image1.Height = NewImgHi : Image1.Width = NewImgWi
 
  Hfactr = NewImgWi - Frame1.Width: If Wfactr < 900 Then Wfactr = 900
  Vfactr = NewImgHi - Frame1.Height: If Vfactr < 900 Then Vfactr = 900
0
 
LVL 29

Author Comment

by:gowflow
ID: 35094565
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
0
 
LVL 5

Assisted Solution

by:BrianVSoft
BrianVSoft earned 500 total points
ID: 35094928
You can only print an image from a PictureBox.. so..
PicBox1.Picture = Image1.Picture
Printer.PaintPicture PicBox1.Picture, x1 , y1, PrWid, PrHi
 ' x1,y1 are the twip positions of the top left corner
 ' PrWid, PrHi are the desired width and Height

Keep in mind that PrWid = PrHi * AspHW  so you need to think twice about the size on paper
0
 
LVL 29

Author Closing Comment

by:gowflow
ID: 35097807
Tks V much for the patience and great advice
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now